Chapter Four: PHP Basics
Chapter Four: PHP Basics
PHP Basics
What is PHP
• In order to develop and run PHP Web pages three vital components need
• The three software (Apache, PHP, and MySQL) is combine together in Vertigo
or Wamp server.
• For example, we can run Wamp web server in the directory “C:\wamp64\www”.
• Hence, we should save our PHP files in www folder.
Basic PHP syntax
• Four ways to differentiate PHP code from other elements in the page.
• Scope can be defined as the range of availability or the part of the script where the variable
can be referenced/used.
• PHP variables can be one of three cope types: Local variables, Global variables and Static
variables
• A variable declared in a function is considered local; that is, it can be referenced solely in
that function.
• Any assignment outside of that function will be considered to be an entirely different
variable from the one contained in the function
<?
$x = 4;
function assignx() {
$x = 0; // this is local variable
print "\$x inside function is $x. ";
}
assignx();
print "\$x outside of function is $x. "; • Output:
?> $x inside function is 0.
$x outside of function is 4.
Scope …
• In contrast to local variables, a global variable can be accessed in any part of the program.
• However, in order to be modified, a global variable must be explicitly declared to be global in the
function in which it is to be modified.
<?
$somevar = 15;
function addit() {
GLOBAL $somevar; //use GLOBAL for global variables
$somevar++;
print "Somevar is $somevar";
}
addit();
?>
• This will produce the following result.
Somevar is 16
Scope …
• Normally, when a function is completed/executed, all of its variables are deleted.
• However, sometimes we want a local variable NOT to be deleted if we need it for a
further job. To do this, use the static keyword at the time of declaration
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
• This will produce the following result.
012
PHP Output Statement
• As shown above PHP has different syntaxes but for maximum compatibility, it is
recommended to use <?php … ?>
• Each code line in PHP must end with a semicolon which is a separator and is used
to distinguish one set of instructions from another.
• There are two basic statements to output text with PHP: echo and print.
• echo can take multiple parameters (although such usage is rare) but print can
only take one argument.
• echo is marginally faster than print.
• The echo or print statement can be used with or without parentheses: echo or
echo ().
• The general format of the echo statement is as follows:
• echo outputitem1, outputitem2, outputitem3, . . .;
• echo (output statement);
• print (outputstatement);
Datatypes in PHP
<?php
if( $_GET["name"] || $_GET["age"] )
{
echo "Welcome ". $_GET['name']. "<br/>";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
Form submission with GET
Form submission with POST
Error Reporting
PHP Control Structures
• In PHP, references enable accessing the same variable content by different names
• In PHP, variable name and variable content are different, so the same content can
have different names.
• A reference variable is created by prefixing & sign to original variable.
• Hence $b=&$a will mean that the address of a is assigned to b.
<?php
$var1 =10;
$var2 =&$var1 ;
echo "$var1 $var2\n";
$var2 =20;
echo "$var1 $var2\n";
• Output:
?> 10 10
20 20
PHP Arrays
• A variable is a storage area holding a number or text.
• The problem is, a variable will hold only one value.
• In PHP, the array() function is used to create an array.
• An array is a special variable that stores one or more similar type of
values in a single variable.
• Three kinds of arrays:
• Numeric array - An array with a numeric index.
• Associative array - An array where each ID key is associated with a value
• Multidimensional array - An array containing one or more arrays and values
are accessed using multiple indices
Numeric Array
<?php • Second example for creating arrays
/* First method to create array. */ <?php
/* Second method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
$numbers[0] = "one";
echo "Value of the array:";
$numbers[1] = "two";
foreach( $numbers as $value )
$numbers[2] = "three";
{ $numbers[3] = "four";
echo "$value "; $numbers[4] = "five";
} echo "Value of the array:";
?> foreach( $numbers as $value )
{
echo "$value ";
}
?>
Associative Arrays