Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Name:__Tan Wan Sean

__________________________________ Date: ______4/8/2020__________

Tutorial P1 – Intro to PHP (1.5 hours)


Aim: To understand how PHP works

 You can use an online PHP server or XAMPP


 If using Xampp ensure that it is installed in your PC. Open XAMPP Control Panel and
ensure that Apache, MySql and Filezilla are running (in green).

1. First write your first php page using Notepad. Check to see the page is working fine.
Save it as page.php inside folder C:XAMPP/htdocs/.

<html>
<head>
<title>My first PHP page</title>
</head>
<body>

<?php

echo "<h1>Hello World!</h1>";

?>

</body>
</html>

2. Make sure XAMPP is running. Open your browser and type http://localhost/ in the address bar.
If XAMPP is running you will see a Welcome page. Now type http://localhost/page.php - What
do you see? Also, comment about the source code when you view the source code.

When we view the source code, we cannot see the PHP code. Whatever enclosed in the PHP
code, it cannot be seen.
Name:__Tan Wan Sean
__________________________________ Date: ______4/8/2020__________

3. Add the code to display the server’s date into page.php. Save and view.

echo date("r");
echo "<p>Today it's " . date("l") . "</p>";

4. We can also use predefined variables as follows. Insert the following inside the php section::

$file = $_SERVER['PHP_SELF'];

$user = $_SERVER['HTTP_USER_AGENT'];

$address = $_SERVER['REMOTE_ADDR'];

The above defines the present file you are using, the web browser you are using and your IP
address as seen by the server.

5. This is how you can display the variables:

echo "<p>You are running the file <b>$file</b>.</p>\n";


echo "<p>You are viewing this page using:<br /><b>$user</b><br />from the IP
address:<br /><b>$address</b></p>\n";

6. Comment about the php file when viewed from your browser and also the source code:
Name:__Tan Wan Sean
__________________________________ Date: ______4/8/2020__________

The code in the php appeared in the source code by uploading the php file.

7. Find what the following codes do:

echo date("y") - Shows the 2 digits of the year


echo date("m") - Shows a month
echo date("n") - numeric representation of a month (form 01 -12)
echo date("F") - full textual representation of a month (January through
December)
echo date("d") - represent the day of the month
echo date("l") - the weekday of today (Monday, Tuesday etc.)

Can you combine them all in one line? Write you code below:
<?php
echo "Today is " . date("l, y/m/d, F-(n)") . "<br>";
?>

Screenshot your output:


Name:__Tan Wan Sean
__________________________________ Date: ______4/8/2020__________

Comment on your code:

Today is Monday, 20/08/03, August-(8) is in the source code .

8. Upload your file into InfinityFree server – does it work? Comment about the date/time.

Yes, it works. The date and time are displayed using the echo date function.

Screenshot your output:

References:
1. http://www.w3schools.com/php/
2. http://html.net/tutorials/php/lesson1.php

You might also like