New PHP Notes
New PHP Notes
What is PHP
The PHP Hypertext Preprocessor (PHP) is a programming language that allows web
developers to create dynamic content that interacts with databases. PHP is basically
used for developing web based software applications.
PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995.
PHP Features
Performance:
1|Page
PHP script is executed much faster than those scripts which are written in other
languages such as JSP and ASP.
Open Source:
PHP source code and software are freely available on the web.
PHP has easily understandable syntax. Programmers are comfortable coding with it.
Embedded:
PHP code can be easily embedded within HTML tags and script.
Platform Independent:
PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP
application developed in one OS can be easily executed in other OS also.
Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy
to create a simple PHP example. To do so, create a file and write HTML tags + PHP
code and save this file with .php extension.
All PHP code goes between the php tag. It starts with <?php and ends with ?>. The
syntax of PHP tag is given below:
<?php
//your code here
?>
PHP Echo
PHP echo statement can be used to print the string, multi-line strings, escaping characters,
variable, array, etc.
2|Page
o echo does not return any value.
o We can pass multiple strings separated by a comma (,) in echo.
o echo is faster than the print statement.
PHP Print
PHP print statement can be used to print the string, multi-line strings, escaping
characters, variable, array, etc.
PHP Variables
o As PHP is a loosely typed language, so we do not need to declare the data types of
the variables. It automatically analyzes the values and makes conversions to its
correct datatype.
o After declaring a variable, it can be reused throughout the code.
o Assignment Operator (=) is used to assign the value to a variable.
$variablename=value;
o A variable must start with a dollar ($) sign, followed by the variable name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
PHP data types are used to hold different types of data or values. PHP supports 8
primitive data types that can be categorized further in 3 types:
3|Page
1. Scalar Types (predefined)
2. Compound Types (user-defined)
3. Special Types
It holds only single value. There are 4 scalar data types in PHP.
1. boolean
2. integer
3. float
4. string
It can hold multiple values. There are 2 compound data types in PHP.
1. array
2. object
1. resource
2. NULL
PHP Boolean
Booleans are the simplest data type works like switch. It holds only two values: TRUE
(1) or FALSE (0).
PHP Operators
Arithmetic Operators
4|Page
Show Examples
A + B will give
+ Adds two operands
30
A - B will give -
- Subtracts second operand from the first
10
A * B will give
* Multiply both operands
200
5|Page
Checks if the value of left operand is less than or
(A <= B) is
<= equal to the value of right operand, if yes then
true.
condition becomes true.
Logical Operators
There are following logical operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then −
Show Examples
Operator Description Example
C = A + B will
Simple assignment operator, Assigns values
= assign value of A +
from right side operands to left side operand
B into C
6|Page
left operand with the right operand and
to C = C / A
assign the result to left operand
PHP Comments
PHP comments can be used to describe any line of code so that other
developer can understand the code easily. It can also be used to hide any code. PHP
supports single line and multi line comments.
In PHP, we can comments multiple lines also. To do so, we need to enclose all lines
within /* */. Let's see a simple example of PHP multiple line comment.
The $var (single dollar) is a normal variable with the name var that stores any value
like string, integer, float, etc.
The $$var (double dollar) is a reference variable that stores the value of the $variable inside
it.
Example 1
7|Page
<?php
$x = "abc";
$$x = 200;
echo $x."<br/>";
echo $$x."<br/>";
echo $abc;
?>
PHP Variable Scope
The scope of a variable is defined as its range in the program under which it can be
accessed. In other words, "The scope of a variable is the portion of the program
within which it is defined and can be accessed."
1. Local variable
2. Global variable
Local variable
The variables that are declared within a function are called local variables for that
function. These local variables have their scope only in that particular function in
which they are declared. This means that these variables cannot be accessed outside
the function, as they have local scope.
Global variable
The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program. To access the global variable
within a function, use the GLOBAL keyword before the variable.
8|Page