DYNAMIC WEB DEVELOPMENT
WEEK 3.A: INTRODUCTION TO PHP
Correspond to Ch 1
Dr. Basel Almourad
GOALS
Learn PHP Basics
OUTLINE
1. Basic syntax
2. Sending Data to Browser
3. Adding Comments
4. Variables
5. Basic types: Strings, Numbers,
Constants
6. Basic Debugging Steps
7. Q & A 2
BASIC SYNTAX
How to add PHP to a web page?
Place it within PHP tags: first.php
<?php <!DOCTYPE html>
<html>
?> <head>
<title></title>
PHP file extension </head>
<body>
.php is the <?php
echo "My first PHP Project";
echo "Second line.";
echo "Third line.";
?>
</body>
</html>
3
BASIC SYNTAX
To add comment, 3 options: second.php
<body>
1. /* one-line or multi-line comment <!–My HTML comment-->
goes here */ <h1> 2nd test</h1>
<?php
2. //one line comment goes here
/* this is my first script */
3. #one line comment goes here echo "My first PHP Project";
echo "Second line.";
#second comment
HTML comment echo "Third line.";
//third comment
<!–comment goes here --> ?>
</body>
4
SENDING DATA TO THE WEB
BROWSER
PHP has a number of built-in functions; e.g.
echo echo ''Hello, world!'';
print print 'Hello, world!';
semicolon
We will mostly use echo
PHP is case-insensitive for functions
echo ≡ ECHO ≡ eCHo ≡ Echo
5
SENDING DATA TO THE WEB
BROWSER
PHP has a number of built-in functions; e.g.
echo echo ''Hello, world!'';
print print 'Hello, world!';
can use either single or double quotation marks
Single and double quotation marks have some differences
will talk about some of them soon…
6
SENDING DATA TO THE WEB
BROWSER
How to print single or double quotation marks?; e.g.
2 options
Use different quotation marks: e.g. echo 'the book author is
"James B. "’;
Use same quotation marks, with escape: e.g. echo "the
book author is \"James B. \" "; 7
VARIABLES
PHP supports eight main types of variables
• Four scalar (single-valued)
• Boolean
• integer
• floating point
• strings
• Two non-scalar (multivalued)
• arrays
• objects
Other types
resources : to interact with database
NULL
8
VARIABLES
A variable name should start with $
e.g. $name
All variable names follow same rules
• Can contain letters, numbers, and underscore
• E.g. $my_report1.
• 1st character after $ should not be a number
• Variable names are case-sensitive
• $name and $Name are different
9
VARIABLES
Example:
third.php
<body>
<?php
$message = "welcome to our first tutorial";
echo $message;
echo "<br />Hello, $message. We are pleased to meet you";
?>
</body>
echo 'Hello,
$message'; // Won't
work!
10
VARIABLES
Some predefined variables
fourth.php
<body>
<?php
$file = $_SERVER['SCRIPT_FILENAME’];
$user = $_SERVER['HTTP_USER_AGENT’];
$server = $_SERVER['SERVER_SOFTWARE’];
// Print the name of this script:
echo "<p>You are running the file:<br /><b>$file</b>.</p>\n";
// Print the user's information:
echo "<p>You are viewing this page using:<br /><b>$user</b></p>\n";
// Print the server's information:
echo "<p>This server is running: <br /><b>$server</b>.</p>\n";
?>
</body> 11
STRINGS
A quoted chunk of characters
Letters, numbers, space, punctuation, etc. e.g.
• ‘Tobias’
Some useful usage for
• “In watermelon sugar”
escape
• ‘100’
• ‘August 2, 2011’
String variable
$today = ‘January 28, 2019’;
If the same quotation mark appears
in the string it must be escaped
e.g. $var = "Define \"platitude\", please.";
$var = 'Define "platitude", please.';
12
STRINGS
What will be the output of the code below?
$name = “Amanda”;
$name = “David”;
echo ”$name”;
echo $name;
echo ‘$name’;
13
CREATING AND USING
STRINGS
$first_name = 'Tobias';
$today = 'August 2, 2011';
$var = "Define \"platitude\", please.";
$var = 'Define "platitude", please.';
echo $first_name;
echo "Hello, $first_name";
STRINGS
use ‘.’ to concatenate strings fifth.php
<body>
<?php
$firstName = "Nada";
$lastName = "Mohammed";
$city = 'Abu Dhabi';
$message = $firstName . ' '. $lastName . ' lives in '. $city;
echo $message; ?>
</body>
Output
15
STRINGS
Other functions
$num = strlen('some string'); // 11 number of characters
strtolower( ) makes the string entirely lowercase;
strtoupper( ) makes the string entirely uppercase;
ucfirst( ) capitalizes the first character
ucwords( ) capitalizes the first character of every word.
16
NUMBERS
Examples
$n1 = 80; $n2=3.5; $n3= -4.2398508
No quotation marks
Some other number functions
round( )
a)Round to nearest integer: e.g.
round(3.14)=3
b)round to specified number of decimal
places; e.g. round(3.142857,3)=3.143
number_format()
a)Use thousands comma; e.g.
number_format(20943)=20,943
b)set a specified number of decimal points;
e.g. number_format(20943,2)=20,943.00
17
CONSTANTS
Created using define() function
define ('NAME', value); recommended to use
ALL CAPITAL name
Don’t use $ sign
Can only be assigned a scalar value Examples of
predefined constants:
o PHP_VERSION
Cannot be changed o PHP_OS
Cannot be displayed inside quotations
echo NAME; works
echo “NAME”; doesn’t work
18
BASIC DEBUGGING STEPS
To debug a PHP script:
Make sure:
o the Apache Web Server is running
o each statement is ended by a semi-column
o The opening and closing quotations are used correctly
Check the displayed error message, if any
o In the browser
o In the IDE (e.g. DreamWeaver, NetBeans)
19
BASIC DEBUGGING STEPS
Examples of errors in browser:
This may be the first of many parse errors you
see as a PHP programmer (this one is caused by
the omission of the terminating quotation mark).
20
BASIC DEBUGGING STEPS
Examples of errors in IDE:
One possible cause of a blank PHP page is a
simple HTML error, like the closing title tag here
(it’s missing the slash)..
21
ANY QUESTIONS?
REFERENCES
• PHP
• https://www.w3schools.com/php/default.asp
• http://php.net/manual/en/langref.php