Tutorial 0 7 - PHP Basics
Tutorial 0 7 - PHP Basics
Learning Outcomes
1. Basic PHP Syntax
2. Declare variables
3. Operators
Page 1 of 7
SEHH1016 – Introduction to Internet Technology T07 – PHP Basics
1. Introduction of PHP
• PHP stands for PHP: Hypertext
Preprocessor
• PHP is a server-side scripting
language, like ASP
• PHP scripts are executed on the
server
• PHP is an open source software
• PHP is free to download and use
Why PHP?
• PHP runs on different platforms (e.g.
Windows, Linux, Unix)
• PHP is compatible with almost all
servers used today (e.g. Apache)
• PHP is FREE to download from the
official PHP resource: www.php.net
• PHP is easy to learn and runs efficiently
on the server side
Page 2 of 7
SEHH1016 – Introduction to Internet Technology T07 – PHP Basics
Comments in PHP
[php01_02_comment.php] In PHP, we use
• // to make a single-line comment or
<?php • /* and */ to make a large comment
// This is a comment block.
/* This is Comment Block */
?>
Page 3 of 7
SEHH1016 – Introduction to Internet Technology T07 – PHP Basics
Variables in PHP
• Variables are used for storing a values, like text
<?php strings, numbers or arrays.
$var_name = value; • When a variable is declared, it can be used over
$valid1 = true; and over again in your script.
$1d = false; // invalid • All variables in PHP start with a $ sign symbol.
variable = "no"; • A variable name must start with a letter or an
?> underscore _, can only contain alpha-numeric
characters and underscores (i.e. a-z, A-Z, 0-9, and
_), and should not contain spaces.
?>
Page 4 of 7
SEHH1016 – Introduction to Internet Technology T07 – PHP Basics
String Variables in PHP - strlen() function
php01_04_strlen.php
<?php strlen() function
• The strlen() function is used to return
echo strlen("Hello world!"); the length of a string.
?>
Page 5 of 7
SEHH1016 – Introduction to Internet Technology T07 – PHP Basics
PHP Operators - Comparison Operators
Page 6 of 7
SEHH1016 – Introduction to Internet Technology T07 – PHP Basics
PHP Exercise
1. Use a text editor to type the following. Save the file with name SEHH1016_T7.php, and put
it in the root directory of your Apache Web server. It should be the “htdocs” folder by
default.
<HTML>
<BODY>
<P> First exercise: </P>
<?php
$msg1 = "My first PHP file.<BR>";
$msg2 = "PHP is interesting.";
echo $msg1;
echo $msg2;
?>
</BODY>
</HTML>
2. Start a Web browser. Type in the address bar the URL of your PHP file:
http://localhost/SEHH1016_T7.php. You should now be able to see the two messages
(msg1 and msg2) in the PHP file.
3. Now, in the PHP file, add another variable called “msg3”. Set it with a value “This is a
testing message!”.
4. Modify the PHP file such that the third message is placed below the second message.
View the result using the Web browser.
5. By using another variable called “line”, insert a horizontal line below the three text
messages.
6. Modify the value in “msg2”, so that the font colour of the second message is changed to
blue. (Hint: use the escape character)
7. Save your work and view the result through the Web server with a Web browser.
Page 7 of 7