Google News
logo
PHP Interview Questions
PHP is an acronym for PHP: Hypertext Preprocessor it is a widely-used open source(free).  PHP is a server-side scripting language designed primarily for web development and web applications. Originally created by Rasmus Lerdorf in 1994.
It is used to print a data in the webpage, Example: <?php echo 'Free Time Learn'; ?> , The following code print the text in the webpage.
The output is displayed directly to the browser.
PHP Variables are used for storing data such as numeric values, characters, character strings etc. All PHP variable names must be pre-fixed with a ( $). Variable names are case-sensitive they are ($var and $VAR are two different variables).

Syntax of declaring a PHP valid and invalid variable names :

$varName         // valid
$_varName       // valid
$__varname    // valid
$var_name       // valid
$varName21     // valid
$_var-Name  // invalid contains non alphanumeric character (-)
$_9Var  // invalid - underscore must be followed by a letter
$99Var   // invalid - must begin with a letter or underscore
They are both variables. But $var is a variable with a fixed name. $$var is a variable who's name is stored in $var. For example, if $var contains "message", $$var is the same as $message.
PHP has a total of eight data types which we use to construct our variables  :

Arrays : are named and indexed collections of other values. An array stores multiple values in one single variable.

Booleans : Booleans are the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

Floating - Floating point numbers ("floats", "doubles" or "real numbers") 

Integers : An integer data type is a number of -2, -1, 0, 1, 2, ... Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) 

notation, optionally preceded by a sign (- or +).

Objects : To initialize an object, you use the new statement to instantiate the object to a variable.

Resources : A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions.

Doubles : are floating-point numbers, like 3.14159 or 49.1.

Strings  : A string is sequence(series) of characters. where every character is the same as a byte.

A string literal can be specified in three different ways.

single quoted : 'Hello world!'
double quoted : "Hello world!"
heredoc syntax : strings is by using heredoc syntax ("<<<").

NULL : The NULL is a special data type. NULL value represents that a variable has no value. NULL is the only possible value of type NULL.
We can include a file using "include() " or "require()" function with file path as its parameter.

Syntax : <?php include('css-files.php'); ?>
If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be  issued, but execution will continue.
A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script.  A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

PHP constant : define() 
Let's see the syntax of define() function in PHP.

define(name, value, case-insensitive)

name : specifies the constant name
value : specifies the constant value
case-insensitive : Default value is false. It means it is case sensitive by default.
A string is sequence(series) of characters. where every character is the same as a byte. i.e. used to store and manipulate text.

A string literal can be specified in three different ways.

single quoted : 'Hello world!'
double quoted : "Hello world!"
heredoc syntax : strings is by using heredoc syntax ("<<<").