unit4_php
unit4_php
Platform Independent
What is a PHP File?
Simplicity
Efficiency
Security
Flexibility
Familiarity
PHP Syntax
A PHP script can be placed anywhere in the
document.
<?php
// PHP code goes here
?>
PHP Case Sensitivity
In PHP, keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are not case-sensitive.
In the example below, all three echo statements below are equal
and legal:
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?></body>
</html>
Comments in PHP
<?php
// This is a single-line comment
<?php
$x = 5; // global scope
function myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
A variable declared within a function has a LOCAL
SCOPE and can only be accessed within that
function:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
?>
use the static keyword when you first declare the variable
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
PHP echo and print Statements
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
$x = 1; - Initialize the loop counter ($x), and set the
start value to 1
$x <= 5 - Continue the loop as long as $x is less than
or equal to 5
$x++; - Increase the loop counter value by 1 for each
iteration
• 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);
PHP Arrays
An array stores multiple values in one single variable.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] .
".";
?>
Create an Array in PHP
In PHP, the array() function is used to create an array:
array();
In PHP, there are three types of arrays:
The form request may be get or post. To retrieve data from get request, we
need to use $_GET, for post request $_POST.
Get request is the default form request. The data passed through
get request is visible on the URL browser so it is not secured. You
can send limited amount of data through get request.
PHP Post Form
• Creating Cookies
Object Oriented Programming with PHP
• Classes.
• Objects.
• Inheritance.
• Encapsulation.
Class & Objects
A class is a template for objects, and an object is an instance of class.
Each object has all the properties and methods defined in the
class, but they will have different property values.