Professional Documents
Culture Documents
Lab 9 01052023 054957pm
Lab 9 01052023 054957pm
OBJECTIVE(S) :
1. To introduce with server side scripting language.
2. To able to learn syntax of PHP operators , loops and arrays in PHP.
3. To execute file using Xampp server.
INTRODUCTION
PHP is a powerful tool for making dynamic and interactive Web pages. PHP is the widely-used,
free, and efficient alternative to competitors such as Microsoft's ASP. PHP stands for PHP:
Hypertext Preprocessorit is a server-side scripting language, like ASP, its scripts are executed on the
server. PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, etc.).
it is an open source ,cross platform and free software , free to download and use.
PHP Syntax
PHP code is executed on the server, and the plain HTML result is sent to the browser.
A PHP scripting block always starts with <?php and ends with ?>.
A PHP scripting block can be placed anywhere in the document.
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting
code.
Below, we have an example of a simple PHP script which sends the text "Hello World" to the
browser:
<html>
<body>
<?php
echo 'Hello World.</br>';
print 'good world.</br>
';
echo 'difference b/w echo and print';
echo 'echo does not return any value while print returns true or false
on successful print.'
?>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html
Note: The file must have a .php extension. If the file has a .html extension, the PHP code will
not be executed
Variables in PHP
Variables are used for storing a values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol
In PHP, a variable does not need to be declared before adding a value to it
PHP automatically converts the variable to the correct data type, depending on its value
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 _
PHP Operator
Arithmetic Operators
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
Assignment Operators
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
Logical Operators
|| Or x=6
y=3
(x==5 || y==5) returns false
! Not x=6
y=3
!(x==y) returns true
<?php
$x=array("one","two","three");
foreach ($x as $value) // a slight difference – array as temp_variable
{
echo $value . "<br />";
}
?>
PHP Function
The real power of PHP comes from its functions.
Give the function a name that reflects what the function does.
The function name can start with a letter or underscore (not a number)
<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>
There are three different kind of arrays and each array value is accesed using an IDs which is
called array index.
Numeric Array:
An array with a numeric index. Values are stroed and accessed in linear fashion. These array
can store numbers, strings and object but their index will represent by numbers. By default
array index starts from zero.
<?php
$numbers = array(1 2 3 4 5 );
foreach($numbers as $value)
?>
Associative Arrays:
An array with string as index. This stores element value in association with key values rather
than a strict linear index order. These arrays are similar to numeric arrays in terms of
functionality but they are different in terms of their index. Associative array will have their
index as string so that you can establish a strong association between key and values.
<?php
$salaries = array( "Muhammad" => 2000 , "zara" => 1000, "Ahmed" =>
10000 , "wahid" =>20000);
Multidimentional Arrays
An array containing one or more arrays and values are accessed using multiple indicies. A
multidimentional array each element in the main array can also be an array. And each element
in the sub array can be an array, and so on.values in multidimentional array can be accessed
using multiple index.
$contacts = array(
array(
),
array(
),
array(
);
?>
Exercises
Exercises 1
Write test , and debug PHP script Write function and the code to test them.
\
Departemnent of Computer Sciences 65 Semester BSCS 06
SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
Exercises 2
Write a php program that generate the marks sheet of a student. Store the information of the student
and its marks using associative array. Display the information in an Html table.
Exercises 3
Write an HTML document that includes anchor tag that calls a PHP document. Also write the called
PHP document, which returns a randomly choosen greeting from the a list of five different greetings.
The greetings must be stored as constant strings in the script. A rndom number between 0 and 4 can
be computed with these lines.
Exercises 4