Showing posts with label PHP Interviews Questions. Show all posts
Showing posts with label PHP Interviews Questions. Show all posts

Tuesday 13 November 2018

ThinkPHP SQL queries with examples

ThinkPHP SQL queries with examples

Question: How to create Model Object?
$userObj= D("Common/Users");



Question: How to add simple AND Query?
$map=array();
$userObj= D("Common/Users");
$map['user_type'] = 2;
$map['city_id'] = 10;
 $lists = $userObj
                ->alias("u")                
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to add simple OR Query?
$map=array();
$userObj= D("Common/Users");
$map['u.username|u.email'] = 'email@domain.com';  //username OR email is email@domain.com
 $lists = $userObj
                ->alias("u")                
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to use =, >, < in Query?
$map['id']  = array('eq',1000); //equal to 1000

$map['id']  = array('neq',1000); //Not equal to 1000

$map['id']  = array('gt',1000);//Greater than 1000

$map['id']  = array('egt',1000);//Greater than OR EQual 1000

$map['id']  = array('between','1,8'); //Between 1-8




Question: How to use like Query?
$map=array();
$userObj= D("Common/Users");
$map['name'] = array('like','test%'); //like test%
 $lists = $userObj
                ->alias("u")                
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to use like Query with NOT?
$map=array();
$userObj= D("Common/Users");
$map['b'] =array('notlike',array('%test%','%tp'),'AND'); //Not like %test% and %tp
 $lists = $userObj
                ->alias("u")                
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to use Inner JOIN ?
$map=array();
$userObj= D("Common/Users");
$map['name'] = array('like','test%'); //like test%
 $lists = $userObj
                ->alias("u") 
                ->join(C('DB_PREFIX') . "profile as p ON u.id = p.uid")               
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to use LEFT JOIN ?
$map=array();
$userObj= D("Common/Users");
$map['name'] = array('like','test%'); //like test%
 $lists = $userObj
                ->alias("u") 
                ->join('LEFT JOIN '.C('DB_PREFIX') . "profile as p ON u.id = p.uid")               
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to use RIGHT JOIN ?
$map=array();
$userObj= D("Common/Users");
$map['name'] = array('like','test%'); //like test%
 $lists = $userObj
                ->alias("u") 
                ->join('RIGHT JOIN '.C('DB_PREFIX') . "profile as p ON u.id = p.uid")               
                ->where($map)
                ->order("u.id DESC")
                ->select();



Question: How to use group by and having?
$map=array();
$userObj= D("Common/Users");
$map['name'] = array('like','test%'); //like test%
 $lists = $userObj
                ->alias("u") 
                ->join('RIGHT JOIN '.C('DB_PREFIX') . "profile as p ON u.id = p.uid")               
                ->where($map)
                ->order("u.id DESC")
                 ->group('u.id')                
                 ->having('count(p.id) >0')
                ->select();



Question: How to use count(for total records)?
$map=array();
$userObj= D("Common/Users");
$map['name'] = array('like','test%'); //like test%
 $count = $userObj
                ->alias("u")                 
                ->where($map)                
                ->count();



Wednesday 20 December 2017

Encapsulation in PHP (OOP)

Encapsulation in PHP (OOP)

Question: What is Encapsulation?
The wrapping up of "Data member" and "Member functions" into a single unit (called class) is known as encapsulation.


Question: Describe the Encapsulation?
Encapsulation means hiding the internal details of an object.
OR
Encapsulation is a technique used to protect the information in an object from the other object.


Question: Give Example of the Encapsulation?
class Arithmetic {
    private $first = 10;
    private $second = 50;

    function add() {
        return $this->first + $this->second;
    }

    function multiply() {
        return $this->first * $this->second;
    }

}

//Create the object 
$obj = new Arithmetic( );

//Add Two number
echo $obj->add(); //60
//multiply Two number
echo $obj->multiply(); //500


Friday 10 November 2017

How to replace double quoted string with bold


Question: How to replace double quoted string with bold in PHP?

$description='Hello "dude", how are you?';
echo preg_replace('/"([^"]+)"/', '<strong>$1</strong>', $description);


Output
Hello dude, how are you?




Question: How to replace bracket string with italic in PHP?

$description='Hello (dude), how are you?';
echo preg_replace('/\(([^)]+)\)/', '<i>$1</i>', $description);


Output
Hello dude, how are you?



Tuesday 22 August 2017

How to check if port is active OR Not?

How to check if port is active OR Not?

Question: What is fsockopen?
fsockopen — Open Internet or Unix domain socket connection.


Question: How to check if port is open OR Not in particular IP Address OR Domain?
function pingDomainWithPort($domain='localhost',$port=80){
    $starttime = microtime(true);
    $file      = @fsockopen($domain, $port, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0; //in active

    if (!$file) { 
        $status = -1;  // Site is down

    } else {

        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

pingDomainWithPort($_SERVER['REMOTE_ADDR'],1935);
pingDomainWithPort($_SERVER['REMOTE_ADDR'],1458);
pingDomainWithPort($_SERVER['REMOTE_ADDR'],80);
pingDomainWithPort($_SERVER['REMOTE_ADDR'],8080);

You can pass the domain name or Ip Address.


Thursday 17 August 2017

PHP Technical Interview Questions and Answer for 2 year experienced

PHP Technical Interview Questions and Answer for 2 year experienced

Question: What is the use of the @ symbol in PHP?
It suppresses error messages, means hide the error/warning messages for that statement.
echo $noDefinedVar;



Question: How to enable all error in PHP?
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

Question: How to disable all error in PHP?
ini_set('display_startup_errors', 0);
ini_set('display_errors', 0);
error_reporting(0);

Question: How to convert a variable to string?
$myVar='123456987';
$myText = (string)$myVar; // Casts to string
var_dump($myText);

Question: How to POST request with PHP?
$ch = curl_init();
$postData='var1=value1&var2=value2&var3=value3';
curl_setopt($ch, CURLOPT_URL, "http://mydomain.com/ajaxurl");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

echo $result;die;



Question: Formatting a number with leading zeros in PHP?
echo sprintf('%08d', 1234567); //01234567
echo sprintf('%09d', 1234567); //001234567
echo sprintf('%010d', 1234567); //0001234567



Question: How to Check if PHP session has already started?
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}


Question: Why do we use ob_start()?
define ("FRUITS", serialize (array ("apple", "mongo", "banana","fig")));
$fruits = unserialize (FRUITS);
print_r($fruits);



Question: How to increase PHP Execution Time?
ini_set('max_execution_time', 300);



Question: how do you change the key of an array element?
$array= array ('a'=>"apple", 'm'=>"mongo", 'b'=>"banana",'f'=>"fig");
print_r($array);
$array['bbbbb']= $array['b'];
unset($array['b']);
print_r($array);



Question: How to get last key in an array?
$array= array ('a'=>"apple", 'm'=>"mongo", 'b'=>"banana",'f'=>"fig");
end($array);
echo key($array);//k



Question: Set HTTP header to UTF-8 using PHP?
header('Content-Type: text/html; charset=utf-8');



Question: How to generate a random, unique, alphanumeric string?
echo md5(uniqid(rand(), true));



Question: How to run SSH Commands from PHP?
$connection = ssh2_connect('HOST_OR_IP_ADDRESS', 'PORT');
ssh2_auth_password($connection, 'USERNAME', 'PASSWORD');

$stream = ssh2_exec($connection, 'ls');
if ($stream) {
        echo "fail: unable to execute command\n";
    } else {
        // collect returning data from command
        stream_set_blocking($stream, true);
        $data = "";
        while ($buf = fread($stream,4096)) {
            $data .= $buf;
            $data.="\n";
        }
        fclose($stream);

        echo $data;
    }



Question: What is output of below?
$a = '1';
$b = &$a;
$b = "2$b";
echo $a.", ".$b;

output
21, 21