Php is one of the most demanding web development language, there are many popular frameworks are written in php, like wordpress, drupal etc. demand of good php programmer increasing day by day.
Here we learn some commonly asked PHP developer interview questions answers for beginners and experienced developer.
PEAR is the framework and repository for reusable PHP components, which provides a command line interface to install "php packages" automatically.
echo
is used for displaying the output value for one or more string
Php "echo" can output one or more string, but php "print" can only output one string and returns 1.
We should always use Echo instead of Print, Echo is faster than print because it does not return any value.
PHP is powered by Zend Engine 2.
(__)
and end with a double underscore (__)
. They are case-insensitive.
Examples: __LINE__ , __FILE__ , __DIR__ , __METHOD__
The indexed array elements stored in an indexed form, index starts from 0, look at the example below.
$colors=array("red","green","blue","orange");
In associative array, elements stored with name, like example below
$marks=array("Aniket"=>"70","Manish"=>"65","Jatin"=>"68");
<?php require("subMenu.php"); ?> <?php include("subMenu.php"); ?>
Which means PHP does not need to declare data type of the variable when we declare any variable like many other programming languages C#, Python etc.
We can store any type of values in a variable, after assigning values the variable become of that type.
$$
is used to declare variable variables in PHP.
Indicates the value of a variable is used as the name of the other variable.
$str = "Hello"; $$str = " How Are You"; //declaring variable variables echo "$str ${$str}"; //It will print "Hello How Are You" echo "$Hello"; //It will print "How Are You"
explode()
function is used for spliting a string into an array,
and implode implode()
function is used for naking a string by combining the array elements.
$text = "Hello How are you doing?"; print_r (explode(" ",$text));
gettype()
function to check the data type of any variable.
echo gettype(true).''; //boolean echo gettype('How are you').''; //string echo gettype(100).''; //integer echo gettype(null).''; //NULL
Garbage collection runs on default session directory “tmp”
session.gc_maxlifetime (default value, 1440)
session.gc_divisor (default value, 100)
session.gc_probability (default value, 1)
mysqli_real_escape_string()
function?
escape string mysqli_real_escape_string()
function can be used when we want
to escape special characters from the string that will be used in a SQL statement
$mysqli = new mysqli("localhost","myuser1","myPassword1","mydbname"); // Escape special characters, if any $firstname = $mysqli -> real_escape_string($_POST['firstname']); $lastname = $mysqli -> real_escape_string($_POST['lastname']);
Htmlentities()
function to prevent cross-site scripting attack.
$mystr = 'Go to WebTrainingRoom.Com'; echo htmlentities(mystr);
#
(Unix Shell style single line comment)//
<?php // This is a single-line comment # This is also a single-line comment ?>
The multiple line PHP comment begins with " /* "
and ends with " */ "
.
below is an example.
<?php /* example of a multiple-lines comment block that spans over multiple lines */ ?>
Learn more about php loop with examples
session_start()
function.
Here is an example how we can define session variable in php code.
<?php session_start(); if(!isset($_SESSION['userinfo'] )) { $_SESSION['userinfo'] = "WebTrainingRoom"; } $msg = "Hello ". $_SESSION['userinfo']; $msg .= ", Welcome."; echo ( $msg ); ?>
Learn more about PHP Session Management with example
Superglobals are built-in variables that are always available in all scopes. and we can access those variables from any function, class or file without having to do anything special.
$_GLOBALS
$_SERVER
$_REQUEST
$_FILES
$_ENV
$_COOKIE
$_SESSION
$_GET
$_POST
Learn more about Php superglobal Variables with examples.
ini_get("mysql.default.user")
,
here is an example how we can read database connection related information from ini file.
$db = mysqli_connect(ini_get("mysql.default.user"), ini_get("mysql.default.password"), ini_get("mysql.default.host"));
learn more about how to read Configuration setting from php.ini
<?php $mysqli = new mysqli("localhost", "username", "password", "dbname"); ?>
read more about php mysql connection.
class car { public $model="Not Set"; public $color="Not Set"; public $price=0.0; }
Learn more about how to cerate class and Objects in php code
enctype="multipart/form-data"
in php form?
<form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="fileUpload1" id="fileUpload1"/> <input type="submit" value="Upload" /> </form>
<script> $.ajax( 'make-ajax-call.php', { success: function(data) { alert('AJAX call successful!'); alert('response from the server' + data); }, error: function() { alert('There was some error response from AJAX call!'); } } ); </script>
Read more details about php ajax form submit.