Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

LAB 09: Introduction to PHP

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.

What is a PHP File?


PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or ".phtml

PHP’s case sensitivity


Case sensitive (both user defined and PHP defined)
 variables
 constants
 array keys
 class properties
 class constants
 functions
 class constructors
 class methods
 keywords and constructs (if, else, null, foreach, echo etc.)
PHP Installation
In order to install server u may choose any of the server either wamp or xampp link is given below
 Web Server
WAMP: www.wampserver.com/en/index.php
• XAMPP:
Xampp: Xamppwww.apachefriends.org/
Includes :
- Apache 2.2.11

Departemnent of Computer Sciences 59 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
- MySQL 5.1.36
- PHP 5.3.0
Download the setup and install it. Provide configuration to loacal host port 80 if already in use.
Check your server by running localhost on your browser.

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 _

Departemnent of Computer Sciences 60 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
<?php
$txt1="Hello World!";
$ex=23.34;
$txt2="What a nice day!";
echo $txt1 . ' ' . $txt2. ' '.$ex; echo
'\. Is a concatenation operator'; ?>

PHP Operator
Arithmetic Operators

Operator Description Example Result


+ Addition x=2 4
x+2

- Subtraction x=2 3
5-x

* Multiplication x=4 20
x*5

/ Division 15/5 3
5/2 2.5

% Modulus (division remainder) 5%2 1


10%8 2
10%2 0

++ Increment x=5 x=6


x++

-- Decrement x=5 x=4


x--

Assignment Operators

Operator Example Is The Same As


= x=y 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

%= x%=y x=x%y

Departemnent of Computer Sciences 61 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
Comparison Operators

Operator Description Example


== is equal to 5==8 returns false

!= is not equal 5!=8 returns true

<> is not equal 5<>8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true

Logical Operators

Operator Description Example


&& And x=6
y=3
(x < 10 && y > 1) returns true

|| Or x=6
y=3
(x==5 || y==5) returns false

! Not x=6
y=3
!(x==y) returns true

Conditional Statement in PHP


The conditional statement in php is almost similar to the ones that we have been using in C, Java
etc.
In PHP we have the following conditional statements:
if statement - use this statement to execute some code only if a specified condition is
true if...else statement - use this statement to execute some code if a condition is true
and another code if the condition is false
if...elseif....else statement - use this statement to select one of several blocks of code to be
executed
switch statement - use this statement to select one of many blocks of code to be executed
<?php <?php
$d=date("D"); $x=2;
if ($d=="Fri") switch ($x)
echo "Have a nice weekend!"; {case 1: echo "Number 1";break;
elseif ($d=="Sun") case 2:echo "Number 2";break;
echo "Have a nice Sunday!"; case 3:echo "Number 3";break;
else default:echo "No number between 1
echo "Have a nice day!"; and 3";
?> }
?>

Departemnent of Computer Sciences 62 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
PHP Loops
The Looping mechanism in php is almost similar to the ones that we have been using in C, Java etc.
In PHP we have the following :
while - loops through a block of code while a specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as as specified
condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array

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

Departemnent of Computer Sciences 63 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
PHP Arrays

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)

{ echo “value is $value</br>”; }

?>

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);

echo "salary of muhmmad is".

$salaries['Muhammad']."</br>"; echo "Salary of Zara is"

. $salaries['zara'] ."</br>"; echo "Salary of Ahmed

is". $salaries['Ahmed']."</br>"; echo "Salary of Wahid

is". $salaries['wahid']."</br>"; ?>

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.

Departemnent of Computer Sciences 64 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP
<?php

$contacts = array(

array(

"name" => "Peter Parker",

"email" => "peterparker@mail.com",

),

array(

"name" => "Clark Kent",

"email" => "clarkkent@mail.com",

),

array(

"name" => "Harry Potter",

"email" => "harrypotter@mail.com",

);

// Access nested value

echo "Peter Parker's Email-id is: " . $contacts[0]["email"];

?>

Exercises

Exercises 1

Write test , and debug PHP script Write function and the code to test them.

1. Parameter : an array of numbers


Return value : the average and median of the parameter array

2. Write a PHP program to check whether a number is positive, negative or zero


3. Factorial program in PHP using recursive function

\
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.

#set the seed for mtrand with the number of microseconds


#since the last full second of the clock
mt_srand((double)microtime() * 1000000);
$number = mtrand(0,4); #computes a random integer 0 -4

Exercises 4

Write a Program to create following pattern using for loops:


*
**
***
****
*****
******
*******
********

Departemnent of Computer Sciences 67 Semester BSCS 06


SEL-310: WEB ENGINEERING Lab 09: Introduction to PHP

You might also like