You are on page 1of 21

Chapter 2

PHP Fundamentals
Introduction
• PHP pages are mixtures of three things, namely
contents, HTML code, and PHP script.
• Pages containing PHP script are different from pages
that contain only HTML, and in order to identify them
to the PHP engine, they are saved with the .php suffix.
• A PHP file can’t be stored just anywhere. You need to
place it under the root directory of your web server
(C://xampp/htdocs).

2
Introduction (cont…)
• In the HTML part, you can use all html tags and
attributes as usual.
• Also, in the PHP part you can embed html tags and
attributes to the echo/print statement but ensure to
use single quotations with the attributes.
• The PHP part starts with <?php and ends with ?> and
all PHP codes in between.

3
Generating Output
• Echo statement is used to output text from the server
to your browser.
• You can also use print statement to display text to the
browser.
• The difference between them is that print returns a
value, and echo doesn’t. Also echo can have more
than one parameter (with no parentheses) but print
can only have one parameter.

4
Generating Output (cont…)
• Because both commands are constructs, neither
requires the use of parentheses.
• Also echo cannot be used as part of a more complex
expression, whereas print can.
• For example:
($x < $y) ? print "$x is less than $y" : print "$x is greater than $y"; //it is ok
($x < $y) ? echo "$x is less than $y" : echo "$x is greater than $y"; //parse error

5
Generating Output (cont…)
• Generally it is recommended that you use echo until
you reach such a point in your PHP development that
you discover the need for using print.

6
Coding with Quotation Marks
• PHP is usually used to write HTML code, and HTML
code contains a lot of quote marks itself.
• Single quotes tell PHP to take everything inside the
quotes exactly as it.
• Double quotes give PHP permission to analyze the
text for special characters, like variables. Single quotes
do not allow for this behavior, which is why they are
rarely used in PHP programming.

7
Generating output with heredocs
• A heredoc is simply a type of multiline quote, usually
beginning and ending with the word HERE.
• Heredocs have some great advantages:
Heredocs preserve quote symbols.
Variable interpolation is supported.
The contents of a heredoc feel like ordinary HTML.
• A heredoc is opened with three less-than symbols
(<<<) followed by a heredoc symbol that will act as a
“superquote”
8
Generating output with heredocs
• A heredoc symbol can be denoted by almost any text,
but HERE is the most common delimiter.
• The closing word for the heredoc must be on its own
line with no other symbols or spaces, just the word
followed by a semicolon.
• You can’t indent the closing word for the heredoc;
there can’t be any spaces or tabs preceding or
following the closing word.

9
Heredoc Example
<?php
$author = "Brian W. Kernighan";
print <<<HERE
<p align = "left">
Debugging is <b>twice</b> as hard as writing the code in the first place.<br>
Therefore, if you write the code as cleverly as possible, you are, by definition,
not smart enough to debug it.
</p>
- $author
HERE;
?>
10
How to Insert Comments in PHP
• Multi-line comment
Use /* at the start and */ at the end. For example:
/* This is a C like comment */
• Single line comment
Use double forward slash (//) at the point you want to add
comment:
//This is a C++ like comment which ends at the end of the line
• You can also use pound or hash (#) symbol. For example:
#This is a shell like comment which ends at the end of the line
11
Variables
• A variable is a location in the computer's memory
where a value can be stored for use by a program at
the run time.
• You do not need to declare variables before using
them, you do not need to declare their type and a
variable can change the type of its value as much as
you want.
• Variables in PHP are preceded with a dollar ($) sign.

12
Variables Creating Rules
• Variable names must start with a letter of the
alphabet or the _ (underscore) character.
• Variable names can contain only alphanumeric and _
(underscore).
• Variable names may not contain spaces.
• Variable names are case-sensitive.
• Variable names cannot be reserved words (like print,
echo, etc).
13
Examples
$count
$_Obj
$A123

14
PHP Case Sensitivity
• Variable names are case-sensitive.
• In PHP, all keywords (e.g. if, else, while, echo, etc.),
classes, functions, and user-defined functions are NOT
case-sensitive.

15
Basic Data Types
• Variables can contain values of any of these data types
without explicitly declaring their type. The variable
“behaves” according to the data type it contains.
Integers
Floating-Point Numbers (real numbers)
Strings
Booleans
Null

16
Constants
• Constants are similar to variables except that you cannot
change their values in the program.
• The names for constants have the same rules as PHP
variables except that they do not have the leading dollar sign
($).
• The main two things you have to remember about constants
are:
They must not be prefaced with a $ (as with regular
variables)
You can define them only using the define function.
17
Constants (cont…)
• To define a constant, use the following syntax:
define("CONSTANT_NAME", value, [case_sensitivity])
• Where:
"CONSTANT_NAME" is a string.
value is any valid PHP expression excluding arrays
and objects.
case_sensitivity is a Boolean (true/false) and is
optional. The default is true (0).

18
Constant examples
define("MY_OK", 0);
//case sensitivity is true because default is true (0)
echo (MY_OK);
define("MY_ERROR", 1, 1);
//case sensitivity is false (1)
echo (MY_error);
• Note that, it is generally considered a good practice to use
only uppercase for constant variable names, especially if
other people will also read your code.
19
Operators
• Assignment operator ( = )
• Arithmetic Operators (+, -, *, /, %)
• Arithmetic Assignment Operators (+=, -=, *=, /=, %=)
• Comparison Operators (<, <=, >, >=, ==, !=)
• Logical Operators (&&, ||, !)
• Concatenation Operator (.)
• Increment/Decrement Operators (++, --)
• The One and Only Ternary Operator (? :)

20
END

21

You might also like