Web Development 1
PHP Basics
PHP Basics
Learning Objectives
At the end of the module the student is expected to:
1. Understand how to use PHP Syntax
2. Understand and apply comments in PHP codes.
3. Understand the concept of case sensitivity in PHP codes.
4. Understand the use of variables.
5. Understand and apply the use of echo and print statements
6. Understand the concept of data types and use it in PHP programs
7. Apply string concatenation
INTRODUCTION
In the previous module, we have learned how to install and run
PHP for the first time. In this module we will learn PHP basics such as
syntax, comments, variables and more.
PHP BASICS
PHP Syntax
To embed PHP code, you must use the following syntax.
php
// PHP code goes here
?>
Every time you want to use php scripts, the codes must be
enclose inside <?php ?> , all codes that are place inside the php block
are read by the php.
Course Module
Web Development 2
PHP Basics
PHP Comments
A comment in PHP code is a line that is not read/executed as
part of the program. Its purpose is to provide information or details to
the codes.
The following are the use of comments.
1. Let developers understand the code you are writing.
2. Serves as a reminder.
Sample Code:
Filename: 02basics_adding_comments.php
The code shows the use of
including comments into a PHP
code.
It helps the user identify the
functions/commands included in
the program.
Here is the output of the
program that add comments to
the PHP code. All comments are
hidden to the user, its only
viewable in the code.
Course Module
Web Development 3
PHP Basics
PHP Case Sensitivity
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are NOT case-sensitive. In the
example below, all three echo statements below are legal (and equal):
Sample Code:
Filename: 03case_sensitivity.php
The code shows the use of case
sensitivity of PHP commands
here is the result of the html
code, it still display the text hello
world even with different case of
the command ECHO.
Note: all variable names are case-sensitive. In the example below, only
the first statement will display the value of the $color variable (this is
because $color, $COLOR, and $coLOR are treated as three different
variables):
Course Module
Web Development 4
PHP Basics
Sample Code:
Filename: 04case_sensitivity.php
The code shows the use of
variables and its case sensitivity.
Since, the only declared variable
is $color, the remaining
variables $COLOR and $coLOR
are not recognized by PHP so it
resulted an error.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the
variable
A variable name must start with a letter or the underscore
character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two
different variables)
Course Module
Web Development 5
PHP Basics
Note: Remember that PHP variable names are case-sensitive
Sample Code:
Filename: 05_PHP_Variables.php
The code below shows the use of
variables in PHP.
Here is result of the code that
uses variables.
PHP is a Loosely Typed Language
PHP automatically converts the variable to the correct data
type, depending on its value. In other languages such as C, C++, and
Java, the programmer must declare the name and type of the variable
before using it.
Course Module
Web Development 6
PHP Basics
PHP Variables Scope
In PHP, variables can be declared anywhere in the script. The
scope of a variable is the part of the script where the variable can be
referenced/used.
PHP has three different variable scopes:
Local - A variable declared within a function has a LOCAL SCOPE and
can only be accessed within that function
Global - A variable declared outside a function has a GLOBAL SCOPE
and can only be accessed outside a function
Static - a static variable will not lose its value when the function exits
and will still hold that value should the function be called again.
PHP 5 echo and print statements
In PHP there are two basic ways to get output: echo and print.
echo and print are more or less the same. They are both used to
output data to the screen. The differences are small: echo has no
return value while print has a return value of 1 so it can be used in
expressions. echo can take multiple parameters (although such usage
is rare) while print can take one argument. echo is marginally faster
than print.
PHP Data Types
Variables can store data of different types, and different data
types can do different things. PHP supports the following data types:
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Course Module
Web Development 7
PHP Basics
The code below shows the
application on the use of
variables in PHP.
This example shows the
application of integer, float
variables.
Filename:
06_Variables1.php
Here is the result of using
variables
Course Module
Web Development 8
PHP Basics
The code shows the use of string
in PHP.
Filename:
07_Variables2.php
Here is the result on using string
variables.
Course Module
Web Development 9
PHP Basics
In this code, shows the
application of variables, it also
show the use of conditional
statement if..else.
Filename:
08_Variables3.php
Here is the result of the
condition. Since the variable
$validUser is set to True, the
output is “User is Valid”.
In the second variable
declaration the $validUser is
now set to False. Now the
output of the code is User is NOT
Valid.
Course Module
Web Development 10
PHP Basics
The code shows the data type of
the variables.
To show the data type, you can
use var_dum();
The var_dump() function is used
to display structured
information (type and value)
about one or more variables.
Filename:
09_Variables3.php
The code snippet shows the data
type of the variable use.
Course Module
Web Development 11
PHP Basics
String Concatenation
The PHP concatenation operator (.) is used to combine 2 sting
values to create one string.
In this code, there are variables
that shows the address of a
person, to combine the address
we use the (.) operator to
concatenate the string
Filename:
10_String_Concatenation.php
Here is the output of the string
concatenation.
Course Module
Web Development 12
PHP Basics
References
Murach, J. (2014) Murach’s PHP and MYSQL (2nd Edition)
Yank, K, PHP and MYSQL: Novice to Ninja
WEBSITE
http://php.net/
http://www.w3schools.com/php/
https://www.tutorialspoint.com/php/
Course Module