0% found this document useful (0 votes)
0 views

Chapter 2-part2

The document provides an overview of PHP programming, covering basic syntax, variable usage, operators, and control structures. It explains how to write PHP code, including comments, variable assignment, and different types of operators and loops. Additionally, it discusses the importance of constants and conditional statements in PHP scripting.

Uploaded by

teerti13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter 2-part2

The document provides an overview of PHP programming, covering basic syntax, variable usage, operators, and control structures. It explains how to write PHP code, including comments, variable assignment, and different types of operators and loops. Additionally, it discusses the importance of constants and conditional statements in PHP scripting.

Uploaded by

teerti13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

DFP50193- WEB PROGRAMMING

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!”;

• Don‟t forget the $ sign at the beginning


of the variable. In that case it will not
work.
PHP is a Loosely Typed Language
• In PHP a variable does not need to be
declared before being set.
• Don‟t have to tell PHP which data type
the variable is.
• PHP automatically converts the variable
to the correct data type, depending on
how they are set.
• In PHP the variable is declared
automatically when you use it.
Variable naming rules
• A variable name must start with a
letter or an underscore "_"
• A variable name can only contain
alpha-numeric characters and
underscores (a-z, A-Z, 0-9, and _ )
• A variable name should not contain
spaces. If a variable name is more
than one word, it should be
separated with underscore
($my_string), or with capitalization
($myString)
Working with variables
<html>
<head>
<title>Working with variables</title>
</head>
<body>

<?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:

It is the year 2008


Control
Structures
• Three (3) types of control structure in
programming
– Sequence
– Selection / Conditional
• If statement
• If..else statement
• Switch statement
– Looping
• For loop
• While loop
• Do..while loop
Conditional If statement
• The „If‟ keyword is used to perform the basic
conditional PHP test to evaluate an
expression for a Boolean value. The
statement will be executed only when the
expression is true

<?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" ;
?>

You might also like