CST463 M3 Ktunotes.in
CST463 M3 Ktunotes.in
PHP
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web
pages.
PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.
• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code is executed on the server, and the result is returned to the browser as plain HTML
PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>
Example
<!DOCTYPE html>
<html>
PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
After the execution of the statements above, the variable $txt will hold the value Hello world!, the
variable $x will hold the value 5, and the variable $y will hold the value 10.5.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a variable. It is
created the moment you first assign a value to it.
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
Variables can store data of different types, and different data types can do different things.
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
Type casting can be achieved by placing the intended types in front of the variable to be cast. The
different type casting operators that can be used to convert the variable from one type to another
are given below.
(array) → array
(string) → string
Here are the some examples of converting one data type to another using type casting.
Type juggling is the feature of PHP where the variables are automatically cast to best fit data type
in the circumstances while manipulating with mathematical operators.
Type juggling converts automatically to the upper level data type that supports for the calculation.
Following is the example that converts string into integer while adding to the integer.
<?php
$increase=”10″; // a string
?>
Here is an another example that shows the PHP’s type juggling capabilities. Hence, the number
used in string can be used to calculate with the integer.
<?php
?>
The following output will be generated from the above PHP code.
[insert_php]
$theory=45;
$practical=”15 is the number obtained”;
$sum=$theory+$practical;
echo “The Total Number Obtained is ” .$sum;
[/insert_php]
PHP Operators
PHP Operator is a symbol i.e used to perform operations on operands. In simple words, operators
are used to perform operations on variables or values. For example:
In the above example, + is the binary + operator, 10 and 20 are operands and $num is variable.
o Arithmetic Operators
o Assignment Operators
o Bitwise Operators
o Comparison Operators
o Incrementing/Decrementing Operators
o Logical Operators
o String Operators
o Array Operators
o Type Operators
o Execution Operators
We can also categorize operators on behalf of operands. They can be categorized in 3 forms:
Flow-Control Statements
PHP supports a number of traditional programming constructs for controlling the flow of execution
of a program.
Conditional statements, such as if/else and switch, allow a program to execute different pieces of
code, or none at all, depending on some condition. Loops, such as while and for, support the
repeated execution of particular code.
if
The if statement checks the truthfulness of an expression and, if the expression is true, evaluates a
statement. An if statement looks like:
if (expression)
statement
To specify an alternative statement to execute when the expression is false, use the else keyword:
if (expression)
statement
else
statement
if ($user_validated)
echo "Welcome!";
else
switch Statement
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
PHP Loops
while Loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
do...while Loop
The do...while loop will always execute the block of code once, it will then check the condition,
and repeat the loop while the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
Parameters:
• test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues.
If it evaluates to FALSE, the loop ends.
foreach Loop
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
For every loop iteration, the value of the current array element is assigned to $value and the array
pointer is moved by one, until it reaches the last array element.