You are on page 1of 28

Working with PHP

1. Software Required for PHP Code running


To run a PHP script, you will need these software’s to complete this tutorial successfully

1. Server: Install the Wamp Server OR Xamp Server

2. Editor : Notepad OR Notepad++ OR Dreamweaver

3. Clint: Any Browser (Internet Explorer, Firefox, Chrome etc)


2. Some Useful Notes about PHP Code

It is important to note following things

a. PHP is case sensitive scripting language

b. Save the PHP scripting file in WWW folder inside the Wamp Server (if you are

working with Wamp Server) OR in htdocs folder if you are using Xamp as your Server

c. It is important to note that PHP script is written between the php tag as shown in the next

figure

<?php

?>

d. We can embed the PHP tag inside the HTML file


3. What is PHP?
First PHP program

1. Type the following code in your chosen editor.

2. Every statement of PHP script must be terminated with semicolon.

3. Save the file in the WWW folder(as we are using Wamp Server)
WWW folder location is chosen

Save As Type: PHP is selected

4. Now, open the Browser and type localhost in the address bar, this will open the default

page for the Wamp server


5. Next, type your PHP file name as

localhost\Test.PHP

6. This will show you the output

Congratulations! You have successfully run the PHP

first test Page.

In the first program, we have used the


echo "Aslam-o-Alaikum";
echo is used to output the Text enclosed inside the quotes “ ”.

we can also use the print statement instead of echo statement to output the
messages as well.

Replace the echo statement with the following print statement, save the file and run
the browser, you will see the same output as echo statement has presented with
you.

print "Aslam-o-Alaikum";
Embedding/Adding the HTML tags inside the echo statement

We can embed all the HTML tags inside the echo statement of PHP. This is shown
in the next Script.

Open the previously saved file , i.e. Test.php, and add some more code in it as
shown

You can see that we have added some HTML tags inside the echo statement. This
demo also shows the usage of print statement discussed above.
Variables in PHP

1. In PHP variables are declared by prefixing their name with dollor sign ($)

2. As PHP is case sensitive, so $no1, $No1 Or $NO1 are three different

variable names in PHP

3. Example for using Variables in PHP

4. Open the above saved Test.PHP file and modify its code as shown

5. Save and run the file in the Browser


6. One thing is important to note in the above code

echo "Sum=".$sum;

. Operator to concatenate string to numeric data


Decision Making in PHP

if statement in PHP
if statement in PHP as the same syntax as in C/C++.

The Syntax of simple if statement in PHP

if(Condition)

Statement1;

Second syntax of if statement

if(Condition)
{
Statement1;
Statement1;
}
Example:
If-else statement in PHP
The Syntax of simple if-else statement in PHP

if(Condition)

Statement1;

else

Statement1;

Example:
The Syntax of simple if-else statement in PHP
if(Condition)
{
Statement1;
Statement2;
}
else
{
Statement1;
Statement2;
}
Example:
Nested if-else statement in PHP
Nested if-else structure Example
Switch statement in PHP
Example:
Loops in PHP
for loop
for loop in PHP as the same syntax as in C/C++.

Example:

Save and run the script. The sample output is shown by:
while loop in PHP
do-while loop in PHP
Example:

*Decision Making and Looping is as easy as in C/C++

* You are require to practice all the Looping Programs of Programming

Fundamentals/OOP in PHP
Passing Data from HTML to PHP Web Page
Design these two forms

Save it asCustomerRegistraiton.html and its code is shown here

<html>
<body>
<b>Customer Registration </b><br/>
<form action="Test.php" method="Post">
<Table width="100%">
<tr>
<Td width="15%">Name: </td>
<td><input type="text" name="txtCustomerName" /></td>
</tr>
<tr>
<td>Cell# </td>
<td><input type="text" name="txtCustomerCellNo" /></td>
</tr>
<tr>
<Td>Email address: </td>
<td><input type="text" name="txtCustomerEmailAddress" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="txtCustomerCity" /></td>
</tr>
<Tr>
<td></td>
<td><input type="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

Code the PHP page and save it as CustomerRegistationStatus.PHP

<html>
<body>

<?php
echo"<b>Customer with following details has been Registered
Successfully................<b /><br />";

echo "Name:".$_POST["txtCustomerName"];
echo"<br/>";

echo "Cell#:".$_POST["txtCustomerCellNo"];
echo"<br/>";

echo "Email:".$_POST["txtCustomerEmailAddress"];
echo"<br/>";

echo "City:".$_POST["txtCustomerCity"];
?>

</body>
</html>
Run the HTML

http://localhost/CustomerRegistraiton.html
Fill in the form, press the Submit button

Congratulations! You have successfully implemented the task


PMAS Arid Agriculture University Rawalpindi
University Institute of Information Technology

Theory Paper Final Term Examination-FALL-2015

MIT 1st (Morning/Evening)

Web Design & Development

CS-709

Maximum Points: 30 Total Time: 120 Minutes

Instructions: Attempt all Questions. Attempt MCQs on Question paper and return the
same in First 20 Minutes.

Question N0. 1 [Marks : 05]

1. Cookies are always stored on Server to keep the track of Web User activities.
[True/False]

2. We can use foreach loop to find the Maximum number from one dimensional Array
[True/False]
3. We can design dynamic and responsive web sites using HTML [True/False]
4. Internal CSS once designed in one web page, can be used in all other web pages of web
site. [True/False]
5. In PHP, string variable is declared using the following special syntax: [True/False]
String $strVar = “Hello! This is a String Variable in PHP”;

6. PHP session is started by a call to the function PHP_SessionStart() [True/False]


7. mysql_select_table() function is used to select the table for either to
Insert/Update/Delete or Select the date from. [True/False]
8. <style> tag is used to include external CSS in PHP web site. [True/False]
9. Correct all the errors from the below PHP code and then find What will be the output of
the following PHP code
<?php
Function myFunction($x=10, Y)
{
echo”value of $X is=” $x;
}
myfunctio(5);
myfunction(10,45)
?>

Question N0. 2 Suppose there is a MySQL database named MyDatabase, the name of database
server is DatabaseServer, User Name for the database server Abdullah and Password is
“Farooqxy%”. There is one table named tbl_Projects (ProjectSID, ProjectTitle,
ProjectSupervisor,CellNo,Email). You are required to design a HTML page to get the value for
each of these attributes. Then finally post the data to next page and store the input data to
database. Also, confirm Project Title should not be NULL, Email must be correct. [Marks : 08]

Question N0. 4 Briefly describe each of the following by providing appropriate programming
examples

a. Design a PHP function that returns the maximum of two Numbers. Numbers must be
passed to function.
b. Differentiate between alert() & document.write() functions in JavaScript.
c. Write a simple PHP code to shown how concatenation between string and number
variable is done in PHP.
d. Differentiate between Inline CSS & Internal CSS. Give Examples.
e. Differentiate between GET & POST methods used for FORM data submission.
[Marks : 10]
Question N0. 3 Why Associative array is used in PHP? Suppose following data is stored in an
Associative array named Sales. Following data is stored in this array against each item as shown
[Marks : 07]

Item Nam Total Sale You are required to


Amount 1. Store these details in above Associative array
Laptops 5000 2. Provide an input textbox to user, where user can enter
LCDs 1000 the Sale Amount
Monitors 3000 3. Add a button with the input textbox, when user clicks
CDs 1000 the button then all the items in the associative array
USBs 3000 must be displayed whose price is greater than the user
RAMs 1000 enter amount.
Mobiles 10,000 4. You are also required to find the Grand Sale Amount
at the end of output
Hint:

For example, if user enters 3000 in the textbox and click the button then all the items in the
Associative array with total sale amount greater than 3000 must be displayed on screen.

*****Good Luck*****
PMAS Arid Agriculture University Rawalpindi
University Institute of Information Technology

Practical-Final Term Examination-FALL-2015


MIT 1st (Morning/Evening)

Web Design and Development--- Practical

CS-
Registration No.________________ Total Time:
60 Minutes

Maximum Points: 2

-----------------------------------------------------------------------------------

Instructions:

 Read the scenario carefully and write the complete code.


Question No. 1 Discuss the main module and functionality of your Semester Assigned
Project.

Question No. 2 Consider the following Table in MySQL database.

Database Name : eHealth

Table Name : tbl_Patients (PatientID, PatientName, PatientAge,Cell#)

You are required to display all those patient details in tabular format in PHP page whose Age
is between 20 to 30. Your are also required to not show the Primary key value in the output,
instead you can shown the Serial Number before each patient details.

Write neat and clear PHP/HTML code.

*****Good Luck*****

------------------------------------------------------------------------------------------
Compiled & Written By

Dr. Saif Ur Rehman, Assistant Professor

UIIT, PMAS Arid Agriculture University, Rawalpindi

You can download in PDF as well as Video Lectures

ON

Java, PHP, ASP.Net with C-Sharp, Working With SQL Server, Oracle 10g, Creating Fusion Charts in ASP.Net,

Using Crystal Reports in ASP.Net, FYP Project Ideas, Software Development Latest Jobs

http://www.ASPUiiTBlogSpot.com

Send your Valuable Suggestion & Comments @ Saif@uaar.edu.pk

You might also like