Best 10 books for CTO - What to Read as Chief Technology Officer?
We will introduce you to 10 amazing books we recommend to every CTO! Well, it couldn’t be truer of...
Thus, it becomes the first programming language of many newbies to web development. So, you will receive a large number of applications when you are going to hire a PHP developer.
Yet, how do you select the best developer out of those applicants? To help you with the interview process, we have created a list of 25 PHP questions that can be used to test the PHP knowledge of an applicant covering a wide area. You can guarantee to find a talented PHP developer by asking these questions in the interview.
PHP is a loosely typed programming language, meaning it’s not required to define the data type of the variable when declaring variables. (Ex: variables in C/C++ are strongly typed and must’ve been declared with the data type. ( int myVariable; ) ). You can assign a string to a certain variable at one point so that the variable is a string ($myVar = “Hello”;), and if you decide to assign an integer value to it later, you can do so, changing the same variable type as int ($myVar = 100; ).
$myVar = “Hello”;
echo $myVar; //Result : Hello
$myVar = 100;
echo $myVar; //Result : 100
The GET method submits the data as a part of the URL, making it visible to everyone, which will pose a huge security risk when sensitive data is submitted (passwords). Moreover, data of a GET request can be bookmarked, cached, and stored in the browser history.
On the other hand, the POST method does not submit data as part of the URL, and it’s not cacheable or bookmarkable. Using the POST method is the better choice between these two methods.
Both include and require are used to add another script/file into a PHP file, utilizing re-usability and thus reducing file size and complexity.
Include will copy the data from the linked file when used. It will generate a warning (E_WARNING) if the linked file is unavailable, and then the rest of the script will continue.
Require will generate a Fatal Error if the linked file is not available and will halt the execution of the script.
By using the define() function. define(NAME, VALUE).
Ex :
define(“app_id”, 12546);
echo app_id; //Result : 12546
Yes, while both are comparison operators, the use of == and === will not return the same output.
The == (loose comparison) operator will compare the given values/context regardless of its data type.
Ex:
$userID = 1001;
$userName = “1001”;
if ($userID == $userName ):
echo “Same values”;
else:
echo “Not the same”;
endif;
// Result: Same values
The === (strict comparison) operator will check if the given value and the data type are exactly the same.
Ex:
$userID = 1001;
$userName = “1001”;
if ($userID === $userName ):
echo “Same values”;
else:
echo “Not the same”;
endif;
// Result: Not the same
There are four error types.
Fatal errors: The most critical error type in the code, and it will stop the execution of the script until the issue is fixed.
A fatal error can occur at startup, compile-time, or runtime.
Ex: A call to an undefined function can cause a fatal error.
function myfunc(){
return “this is my function”;
}
echo $getOutput = myfunction();
Parse errors: A parse error is caused by syntax errors in the code. Often it can be triggered by a missing or misused symbol. It will be a compile-time error, and the execution of the script will be stopped when it occurs.
Ex :
$varOne = 1;
$varTwo =
echo $varOne+$varTwo;
Warning errors: Warnings will not stop the execution of the script. Instead, it’ll skip the code with the issue, display the warning and continue the execution. While these errors may seem not critical, they should be treated as important since they can cause more significant issues in the future.
A good example will be the use of include with a missing linked file.
Notice errors: A bit similar to warnings, notice errors are minor issues that can be fixed easily and will not affect the execution of the script.
Ex: undefined variable error.
The __construct() method ( the constructor ) will be automatically called by PHP on each newly created object from a class and used to initialize the object properties.
Ex:
Class DbConnector{
private $dbName;
private $dbUser;
private $dbPass;
public function __construct($dbName, $dbUser, $dbPass){
$this->dbName = $dbName;
$this->dbUser = $dbUser;
$this->dbPass = $dbPass;
}
}
Can be used as,
$connect = new DbConnector(“my_database”, “root”, “”);
By using the array_sum() method. It’ll take the array name as one parameter and return the count of values. It can even be used with an associative array.
$ar = array(2,4,6,8,10,12,14);
$getCount = array_sum( $ar );
echo $getCount;
// Result : 56
Variable is = $_SERVER;
The following code can be used to display the IP address of the server using PHP and $_SERVER[‘SERVER_ADDR’].
$getIP = $_SERVER[‘SERVER_ADDR’];
echo $getIP;
PHP comes with a built-in function named date(), which accepts two parameters, format and timestamp.
$getDate = date(“j / n / Y”);
Yes, while both echo and print seem to behave similarly, there are a few minor differences.
Ex :
$userName = “Elliot”;
echo $userName; // Result: Elliot
print $userName; // Result: Elliot
– Echo has no return value ( return type is void ) while print always has a return value of 1. This return value provides the ability to use print in expressions.
Ex :
$userName = “Elliot”;
$outputEC = echo $userName; // Result: Parse error: syntax error, unexpected T_ECHO
$outputPR = print $userName; // Result: Elliot
echo $outputPR; // Result : 1
– Even though we can provide multiple arguments into echo, print can utilize only one argument.
Ex:
$userName = “Elliot”;
$userRole = “SysAdmin”;
$userLevel = “Level-2”;
echo $userName , $userRole, $userLevel;
//Result : ElliotSysAdminLevel-2
print $userName , $userRole, $userLevel;
//Result : PHP Parse error: syntax error,
-* Echo is somewhat faster than print. (marginally faster).
The error UPLOAD_ERR_INI_SIZE is caused because the uploaded file size exceeds the maximum allowed file size limit in the php.ini configuration file. Change upload_max_filesize (declared in megabytes) in the php.ini file to change the maximum allowed file size.
Ex : upload_max_filesize = 64M
The in_array() function is used to search for a value inside a string. It can be used to perform a case-sensitive search by setting the third parameter as TRUE.
Syntax : in_array(search_term, array_name, type_parameter);
Ex :
$myArray = array(“Elliot”, “Parker”,”Hardison”,8958);
if( in_array(“Elliot”), $myArray, TRUE ):
echo “Elliot is here”;
Endif;
The urlencode() function can be used to achieve this.
Ex :
$safeURL = urlencode (“https://www.google.com/search?q=php+8”);
echo $safeURL;
//Result :
https%3A%2F%2Fwww.google.com% 2Fsearch%3Fq%3Dphp%2B8
It’s possible by adding show error options in the code at the top of the script and enabling error reporting in the .htaccess file.
Include the following lines in your script,
ini_set(‘display_errors’, ‘1’);
ini_set(‘display_startup_errors’, ‘1’);
error_reporting(E_ALL);
Then, include this line in the .htaccess file.
php_flag display_errors 1
By using the final keyword in the class.
class myFirst {
private $myName = “Sheldon”;
}
final class mySecond{
private $myEmail = “[email protected]”;
}
class myThird extends mySecond{
function getMyinfo(){
$myAddress = “address”;
}
// use of extends mySecond will throw an error, PHP Fatal error: Class myThird may not inherit from final class (mySecond)
By using interfaces (or using Traits).
( Ex :
$myVariableOne = 70;
$myVariableTwo = 20;
$myVariableOne += $myVariableTwo;
)
The operator += is a shorthand for adding another value to the original value and saving the final result in the original variable.
In the above example, we can write,,
$myVariableOne += $myVariableTwo;
as
$myVariableOne = $myVariableOne + $myVariableTwo;
So, in the end, the value of $myVariableOne will be 90.
*Works only for numeric values. You should use ( .= ) for strings.
We can use two different methods, which will provide two different results.
We can use the unset() method to remove the element without affecting the array indexes.
$myTeam = [0 => “Leonard”, 1 => “Howard”, 2 => “Sheldon”, 3 => “Raj”];
print_r($myTeam);
//Result : Array ( [0] => Leonard [1] => Howard [2] => Sheldon [3] => Raj )
unset($myTeam[2]);
print_r($myTeam);
//Result : Array ( [0] => Leonard [1] => Howard [3] => Raj )
The element is removed, but array indexes are unchanged.
By using array_splice() method. Unlike the unset() method, array_splice() uses offset and the length of the array. If provided, it removes the elements and reindexes or replaces the elements from the replacement array.
$myTeam = [0 => “Leonard”, 1 => “Howard”, 2 => “Sheldon”, 3 => “Raj”];
print_r($myTeam);
//Result : Array ( [0] => Leonard [1] => Howard [2] => Sheldon [3] => Raj )
array_splice($myTeam, 2, 1);
print_r($myTeam);
//Result : Array ( [0] => Leonard [1] => Howard [2] => Raj )
array_splice($myTeam, 2, 2);
print_r($myTeam);
//Result : Array ( [0] => Leonard [1] => Howard )
By using the ampersand symbol ( & ),
$myVarOne = &$myVarTwo;
Ex :
$myVarOne = “Hello”;
$myVarTwo = “Hello Sheldon”;
echo $myVarOne; // Result : Hello
$myVarOne = &$myVarTwo;
echo $myVarOne; // Result : Hello Sheldon
Self is used to access static methods and properties.
$this refers to the current object, while self refers to the current class. $this ( $this->userName )is used to access non-static methods and properties , while self ( self::$userName ) is used to access static methods and properties.
There is no difference. PHP documentation mentions that die() will be an equivalent to exit(). The method die() is an alias for exit().
Since PHP has a close resemblance to C and Perl, exit() came from C, while die() came from Perl.
As mentioned in the PHP documentation, it’s the error control operator. When it is prepended to an expression, any diagnostic error that might be generated by that expression will be suppressed.
By using the session_status() method.
$getSessionState = session_status();
if( $getSessionState === PHP_SESSION_NONE ):
session_start();
endif;
it’ll return,
PHP_SESSION_DISABLED (or 0 ) if sessions are disabled, PHP_SESSION_NONE (or 1 ) if the session is not active, and PHP_SESSION_ACTIVE (or 2 ) when the session has already started.
The print_r() function will take a variable or expression as a parameter and display structured information about the variable/expression in a human-readable format.
Ex :
$myArray = array( 121, 232, array( “Rick”, “Morty” ) );
print_r($myArray);
//Result
On the other hand, var_dump() will take a variable/expression as a parameter and dump the data with data types in a structured format of type and value. Arrays and objects are explored recursively, with values to show the structure.
Ex:
$myArray = array( 121, 232, array( “Rick”, “Morty” ) );
var_dump($myArray);
//Result
We will introduce you to 10 amazing books we recommend to every CTO! Well, it couldn’t be truer of...
It may come as a surprise to many, but at least half of the programming languages were not created in the...
Laravel architecture was designed for MVC web applications, making it very powerful in terms of business logic and data presentation. The...