Chapter 2-part2
Chapter 2-part2
PHP PROGRAM
PHP Syntax
• PHP code is executed on the server,
and the plain HTML result is sent to
the browser.
Basic PHP Syntax
• A PHP scripting block always starts with
<?php and ends with ?>. A PHP scripting
block can be placed anywhere in the
document.
• On servers with shorthand support
enabled you can start a scripting block
with <? and end with ?>.
• For maximum compatibility, standard
form (<?php) are recommended rather
than the shorthand form.
• A PHP file normally contains HTML tags,
just like an HTML file, and some PHP
scripting code.
• Each code line in PHP must end with a
semicolon. The semicolon 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.
• Note: The file must have the .php
extension. If the file has a .html extension,
the PHP code will not be executed.
Comments in PHP
• In PHP, use // to make a single-line
comment or /* and */ to make a large
comment block.
<html>
<body>
<?php
//This is a comment
/* This
is a
comment
block */
?>
</body>
</html>
PHP Variables
• Variables are used for storing values,
such as numbers, strings or function
results, so that they can be used
many times in a script.
PHP Variables
Variables in PHP
• Variables are used for storing a
values, like text strings, numbers or
arrays.
• When a variable is set it can be used
over and over again in your script
• All variables in PHP start with a $
sign symbol.
The correct way of setting a
variable in PHP:
• Syntax:-
$var_name = value;
• Example:
$marks = 81.5;
$msg = “Hi there!”;
<?php
$name =“Suzana";
echo "Hello $name";
?>
</body></html>
Assigning values to variables
• To assign a value to variable,
regardless of the variable type, use
equal sign (=) called assignment
operator
• For example:-
<html>
<head><title>Assign value to variables</title>
</head>
<body>
<?php
$street = "jln raja musa mahadi";
$city = "ipoh";
$zip = 31400;
echo "The address is: <br> $street
<br>$city $zip";
?>
</body></html>
Operators and
Expressions
Operator, operand and expressions
• An operator is a symbol or series of
symbols that, when used in
conjunction with values, perform an
action and usually produces a new
value.
• An operand is a value used in
conjunction with an operator. There
are usually two or more operands to
one operator.
• An expression is any combination of
functions, values and operators that
resolves to a value.
The assignment operator (=)
• Each time a variable was declared,
the assignment operator consists of a
single character =.
• The assignment operator takes the
value of the right-side operand and
assigns it to the left-side operand.
Arithmetic operators
Operator Name Example Sample Result
+ Addition 10 + 3 13
- Subtraction 10 – 3 7
/ Division 10 / 3 3.33333333333
* Multiplication 10 * 3 30
% Modulus 10 % 3 1
The concatenation operator
• Is represented by a single period(.)
• Treating both operands as string,
this operator appends the right-side
operand to the left-side operand
• For example;
- String + String
"Hello". " World" - String + Variable
returns - Variable + Variable
Hello World
Comparison Operators
• Perform comparative tests using
their operands and return Boolean
value TRUE if the test is successful or
FALSE if the test fails.
• Some of the comparison operators;
==, !=, >, <, >=, <=
Constant
• Variables offer flexible way of
storing data because you can change
their values and the type of data they
store at any time during the
execution of scripts.
• Constant – a value that must remain
unchanged throughout script‟s
execution.
• Using define() function
define(constantName, value);
<?php
define("YEAR", 2008 );
echo " It is the year ".YEAR;
?>
The result:
<?php
$num = 7;
if($num % 2 != 0)
{
$msg = "$num is an odd number.";
echo $msg;
}
?>
If..else statement
• The PHP „else‟ keyword can be used with „if‟
statement to provide alternative code to
execute in the event that the tested expression
is found to be false
<?php
$num = 7;
if($num % 2 != 0)
echo "$num is an odd number.";
else
echo "$num is an even number.";
?>
Switch statement
<?php
$num = 2;
switch($num)
{
case 1 : echo "This is case 1";
break;
case 2 : echo "This is case 2";
break;
case 3 : echo "This is case 3";
break;
default: echo "This is default";
}
?>
For Loop
<?php
$a = 0; $b = 0;
for($i = 0; $i < 5; $i++)
{
$a += 10;
$b += 5;
}
echo "At the end of the loop a = $a
and b = $b" ;
?>
While Loop
<?php
$i = 0; $num = 50;
while($i < 10)
{
$num--;
$i++;
}
echo "Loop stopped at $i<br> \$num is
now $num" ;
?>
Do..while Loop
<?php
$i = 0; $num = 50;
do
{
$num--;
$i++;
}while($i < 1);
echo "Loop stopped at $i<br> \$num is
now $num" ;
?>