7/31/2019 2 - PHP basic
1/76
www.smartosc.com
7/31/2019 2 - PHP basic
2/76
www.smartosc.com
What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL,Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.)
PHP is an open source software PHP is free to download and use
7/31/2019 2 - PHP basic
3/76
www.smartosc.com
What is a PHP File?
PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain
HTML
PHP files have a file extension of ".php",".php3", or ".phtml
7/31/2019 2 - PHP basic
4/76
www.smartosc.com
What is MySQL?
MySQL is a database server
MySQL is ideal for both small and large
applications
MySQL supports standard SQL MySQL compiles on a number of platforms
MySQL is free to download and use
7/31/2019 2 - PHP basic
5/76
www.smartosc.com
PHP + MySQL
PHP combined with MySQL are cross-platform
(you can develop in Windows and serve on a
Unix platform)
7/31/2019 2 - PHP basic
6/76
www.smartosc.com
Why PHP?
PHP runs on different platforms (Windows,
Linux, Unix, etc.)
PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
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
http://www.php.net/http://www.php.net/7/31/2019 2 - PHP basic
7/76
www.smartosc.com
Where to Start?
To get access to a web server with PHP support,
you can:
Install Apache (or IIS) on your own server, install
PHP, and MySQL
Or find a web hosting plan with PHP and MySQL
support
7/31/2019 2 - PHP basic
8/76
www.smartosc.com
I. Setup PHP Environment
http://sourceforge.net/projects/xampp/files/XAMPP%20Windo
ws/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/download
http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/downloadhttp://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/xampp-win32-1.7.7-VC9-installer.exe/download7/31/2019 2 - PHP basic
9/76
www.smartosc.com
II. PHP Syntax
Basic Syntax
1. Comments
2. Data Type
3. Variables
4. Quotes & Special Characters
5. Arrays
6. Operators
7. Conditionals and Loops
8. Functions
7/31/2019 2 - PHP basic
10/76
www.smartosc.com
Basic Syntax
Each code line in PHP must end with a semicolon.
The semicolon is a separator and is used to
distinguish one set of instructions from another.
7/31/2019 2 - PHP basic
11/76
www.smartosc.com
1. Comments in PHP
The first is by using // to comment out a line.
Here is an example:
If you have a single line comment, another
option is to use a # sign. Here is an example of
this:
7/31/2019 2 - PHP basic
12/76
www.smartosc.com
1. Comments in PHP
If you have a longer, multi line comment, the
best way to comment is by using /* */. You can
contain several lines of commenting inside a
block. Here is an example:
7/31/2019 2 - PHP basic
13/76
www.smartosc.com
2. Data Type
Seven (6) basic data types you can work with: null
integer numbers
floating point numbers
booleans
Compound Data Types
strings
7/31/2019 2 - PHP basic
14/76
www.smartosc.com
2. Data Type - null
First the special cases is Null
Null is a special data type which can have only
one value, which is itself. Which is to say, null is
not only a data type, but also a keyword literal.
A variable of data type null is a variable that has
no value assigned to it.
7/31/2019 2 - PHP basic
15/76
www.smartosc.com
2. Data Type - integer numbers
integer: a whole number, a number with no
fractional component. It is a number between -
2,147,483,648 and +2,147,483,647.
decimal: a base ten numbering system
Valid decimal integers:
1
123
+7-1007395
7/31/2019 2 - PHP basic
16/76
www.smartosc.com
2. Data Type - integer numbers
octal: a base eight numbering system
Valid octal integers:010123+07
-01007345 hexadecimal: a base sixteen numbering system
Valid hexadecimal integers:0x10xff0x1a3+0x7-0x1ab7345
7/31/2019 2 - PHP basic
17/76
www.smartosc.com
2. Data Type - Floating Point
Numbers
floating-point number: numbers with a fractional component.
Some examples of valid floating point numbers include:3.140.001
-1.234
0.314E2 // 31.41.234E-5 // 0.00001234-3.45E-3 // -0.00345
If you want to know if a variable contains a floating-pointnumber, you can use the is_float()
7/31/2019 2 - PHP basic
18/76
www.smartosc.com
2. Data Type - Booleans
A Boolean datum can only contain two values: true or false. Generallyspeaking, Booleans are used as the basis for logical operations. Whenconverting data to and fromthe Boolean type, several special rulesapply:
- A number(either integer or floating-point) converted into a Booleanbecomes false if the original value is zero, and true otherwise.
- A string is converted to false only if it isempty or if it contains thesingle character 0.If it contains any other dataeven multiple zerosit is converted to
true.
- When converted to a number or a string, a Boolean becomes 1 if it istrue, and 0 otherwise.
7/31/2019 2 - PHP basic
19/76
www.smartosc.com
2. Data Type - Compound Data
Types
In addition to the scalar data type that we have justexamined, PHP supports two compounddata typesso calledbecause they are essentially containers of other data:
Arrays are containers of ordered data elements; an arraycan be used to store and retrieve any other data type,including numbers, Boolean values, strings, objects and evenother arrays. They are discussed in the Arrays chapter.
Objects are containers of both data and code. They form
the basis of Object-oriented Programming, and are alsodiscussed in a separate chapter called Object OrientedProgramming in PHP.
7/31/2019 2 - PHP basic
20/76
www.smartosc.com
2. Data Type - Strings
In the minds of many programmers, strings are equivalent totext. While in some languages this is, indeed, the case, inmany others (including PHP), this would be a very limitingand, in some cases, incorrectdescription of this data type.
Stringsare, in fact, ordered collections of binary datathiscould betext, but it could also be the contents of an imagefile, a spreadsheet, or even amusic recording.
PHP provides a vast array of functionality for dealingwithstrings. As such,we have dedicated a whole chapter to thementitled, quite imaginatively, Strings.
7/31/2019 2 - PHP basic
21/76
www.smartosc.com
2. Data Type - Converting
Between Data Types
PHP takes care ofconverting between datatypes transparently when a datum is used in anexpression. However, it is still possible to force
the conversion of a value to a specific type usingtype conversion operators.
For example:$x = 10.88;echo (int) $x; // Outputs 10
7/31/2019 2 - PHP basic
22/76
www.smartosc.com
3. Variables
Rules for PHP variable names: Variables in PHP starts with a $ sign, followed by the name of
the variable
The variable name must beginwith a letter or the underscore
character
A variable name can only containalpha-numeric characters and
underscores (A-z, 0-9, and_ )
A variable name should not contain spaces
Variable names are case sensitive (y and Y are two differentvariables)
7/31/2019 2 - PHP basic
23/76
www.smartosc.com
3. Variables
Creating (Declaring) PHP Variables
7/31/2019 2 - PHP basic
24/76
www.smartosc.com
3. Variables
PHP is a Loosely Typed Language In PHP, a variable does not need to be declaredbefore
adding a value to it.
In the example above, notice that we did not have to tell
PHP which data type the variable is.
PHP automatically converts the variable to the correct datatype, depending on its value.
In a strongly typed programming language, you have todeclare (define) the type and name of the variable beforeusing it.
7/31/2019 2 - PHP basic
25/76
www.smartosc.com
3. Variables
PHP Variable Scope
The scope of a variable is the portion of the
script in which the variable can be referenced.
PHP has four different variable scopes:
- local- global
- static
- parameter
7/31/2019 2 - PHP basic
26/76
www.smartosc.com
3. Variables
Local Scope
A variable declared within a PHP function is
local and can only be accessed within that
function. (the variable has local scope):
7/31/2019 2 - PHP basic
27/76
www.smartosc.com
3. Variables
Global Scope Global scope refers to any variable that is defined outside of any
function.
Global variables can be accessed from
any part of the script that is not inside
a function.
To access a global variable from within
a function, use the global keyword:
7/31/2019 2 - PHP basic
28/76
www.smartosc.com
3. Variables
Global Scope PHP also stores all global variables in an array called
$GLOBALS[index]. Its index is the name of the variable. This
array is also accessible from within functions and can be used to
update global variables directly.
7/31/2019 2 - PHP basic
29/76
www.smartosc.com
3. Variables
Static Scope
When a function is completed, all of its variablesare normally deleted. However, sometimes youwant a local variable to not be deleted. To do this,use the static keyword when you first declare the
variable: static $rememberMe;
Then, each time the function is called, that variable
will still have the information it contained from thelast time the function was called.
Note: The variable is still local to the function.
7/31/2019 2 - PHP basic
30/76
www.smartosc.com
3. Variables
Parameters
A parameter is a local variable whose value is
passed to the function by the calling code.
Parameters are declared in a parameter list as
part of the function declaration:
7/31/2019 2 - PHP basic
31/76
www.smartosc.com
4. Quotes & Special Characters
Single quotes (') & Double quotes (")
Unlike some other languages, these two methods behave
quite differently:
- Single quotes represent simple strings, where almost all
characters are used literally.
- Double quotes, on the other hand, encapsulate complex
strings that allow for special escape sequences (for
example, to insert special characters) and for variable
substitution, which makes it possible to embed the value of
a variable directly in a string, without the need for any
special operator:
7/31/2019 2 - PHP basic
32/76
www.smartosc.com
4. Quotes & Special Characters
When single quotes are used, as below, the variable isn't
evaluated and is printed on the screen literally as$something.
In the above, using double quotes, the variable
$something is evaluated within the quotes For make it clear, you can encapsulate the variable in
braces {}.
7/31/2019 2 - PHP basic
33/76
www.smartosc.com
4. Quotes & Special Characters
Removing HTML special characters (including
brackets) PHP has a function called htmlspecialchars which changes some
HTML to special characters, which do not run as a program.
htmlspecialchars changes, for instance, the bracket (
7/31/2019 2 - PHP basic
34/76
www.smartosc.com
4. Quotes & Special Characters
Escaping with the backslash \
The backslash character (\) can be used to escape quotesand variables. The next example repeats the idea thatthings are evaluated within double quotes:
The program doesn't crash on finding more doublequotes, but understands that these are just to emphasize"is". And the program does not explode $something, butprints it literally, because it is escaped.
7/31/2019 2 - PHP basic
35/76
www.smartosc.com
4. Quotes & Special Characters
The Heredoc Syntax A third syntax, called heredoc, can be used to declare complex
stringsin general, the functionality it provides is similar to
double quotes.
Start with
7/31/2019 2 - PHP basic
36/76
www.smartosc.com
5. Array
In PHP, there are three kind of arrays:
Numeric array- An array with a numeric index
Associative array
- An array where each ID key is associated with a value
Multidimensional array
- An array containing one or more arrays
7/31/2019 2 - PHP basic
37/76
www.smartosc.com
5. Array
Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.
7/31/2019 2 - PHP basic
38/76
www.smartosc.com
5. Array
Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical
array is not always the best way to do it.
With associative arrays we can use the values as keys and assign
values to them.
7/31/2019 2 - PHP basic
39/76
www.smartosc.com
5. Array
Multidimensional Arrays
In a multidimensional array, each element in the main array canalso be an array. And each element in the sub-array can be an
array, and so on.
i.e. Automatically assigned ID keys:
7/31/2019 2 - PHP basic
40/76
www.smartosc.com
5. Array
Multidimensional Arrays
The array above would look like this if written to the output:
7/31/2019 2 - PHP basic
41/76
www.smartosc.com
5. Array
Multidimensional Arrays
Lets try displaying a single value from the array above:
7/31/2019 2 - PHP basic
42/76
www.smartosc.com
6. PHP Operators
The assignment operator = is used to assign values to variables
in PHP. The arithmetic operator + is used to add values together.
There are many types of operators in PHP, those commonly used
are:
- Arithmetic Operators- Assignment Operators
- Incrementing/Decrementing Operators
- Comparison Operators
- Logical Operators
- Array Operators
7/31/2019 2 - PHP basic
43/76
www.smartosc.com
6. PHP Operators
Arithmetic Operators
The table below lists the arithmetic operators in PHP:
7/31/2019 2 - PHP basic
44/76
www.smartosc.com
6. PHP Operators
Assignment Operators
The basic assignment operator in PHP is "=". It means that theleft operand gets set to the value of the expression on the right.
That is, the value of "$x = 5" is 5.
7/31/2019 2 - PHP basic
45/76
www.smartosc.com
6. PHP Operators
Incrementing/Decrementing Operators
7/31/2019 2 - PHP basic
46/76
www.smartosc.com
6. PHP Operators
Comparison Operators
Comparison operators allows you to compare two values:
7/31/2019 2 - PHP basic
47/76
www.smartosc.com
6. PHP Operators
Logical Operators
7/31/2019 2 - PHP basic
48/76
www.smartosc.com
6. PHP Operators
Array Operators
7/31/2019 2 - PHP basic
49/76
www.smartosc.com
7. Conditionals and Loops
Conditional Statements In PHP we have the following conditional statements:
if statement - use this statement to execute some code only if aspecified condition is true
if...else statement - use this statement to execute some code if a
condition is true and another code if the condition is false if...elseif....else statement - use this statement to select one of
several blocks of code to be executed
switch statement - use this statement to select one of manyblocks of code to be executed
Ternary Conditionals - Ternary conditionals (sometimes knownas the "trinary operator") are a part of PHP that many simplysteer clear of, despite their usefulness.
7/31/2019 2 - PHP basic
50/76
www.smartosc.com
7. Conditionals and Loops
The if Statement
Use the if statement to execute some code only
if a specified condition is true.
Notice that there is no ..else.. in this syntax. Thecode is executed only if the specified condition
is true.
7/31/2019 2 - PHP basic
51/76
www.smartosc.com
7. Conditionals and Loops
The if...else Statement
Use the if....else statement to execute some
code if a condition is true and another code if a
condition is false.
Syntax
if(condition)
code to be executed if condition is true;
elsecode to be executed if condition is false;
7/31/2019 2 - PHP basic
52/76
www.smartosc.com
7. Conditionals and Loops
The if...elseif....else Statement
Use the if....elseif...else statement to select oneof several blocks of code to be executed.
Syntax
if(condition)code to be executed if condition is true;
elseif(condition)code to be executed if condition is true;
elsecode to be executed if condition is false;
7/31/2019 2 - PHP basic
53/76
www.smartosc.com
7. Conditionals and Loops
The PHP Switch Statement Use the switch statement to select one of many blocks of code
to be executed.
7/31/2019 2 - PHP basic
54/76
www.smartosc.com
7. Conditionals and Loops
Ternary Conditionals Ternary conditionals (sometimes known as the "trinary
operator") are a part of PHP that many simply steer clear of,
despite their usefulness.
C
7/31/2019 2 - PHP basic
55/76
www.smartosc.com
7. Conditionals and Loops
PHP Loops In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition
is true
do...while - loops through a block of code once, and then
repeats the loop as long as a specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an
array
7 C di i l d L
7/31/2019 2 - PHP basic
56/76
www.smartosc.com
7. Conditionals and Loops
The while Loop
The while loop executes a block of code while a
condition is true.
Syntax
while (condition) {
code to be executed;
}
7 C diti l d L
7/31/2019 2 - PHP basic
57/76
www.smartosc.com
7. Conditionals and Loops
The do...while Statement
The do...while statement will always execute the
block of code once, it will then check the
condition, and repeat the loop while the
condition is true.
Syntax
do {
code to be executed;
} while (condition);
7 C diti l d L
7/31/2019 2 - PHP basic
58/76
www.smartosc.com
7. Conditionals and Loops
The for Loop
The for loop is used when you know in advance
how many times the script should run.
Syntax
for (init; condition; increment) {
code to be executed;
}
7 C diti l d L
7/31/2019 2 - PHP basic
59/76
www.smartosc.com
7. Conditionals and Loops
The foreach Loop
The foreach loop is used to loop through arrays.
Syntax
foreach ($arrayas$value) {
code to be executed;
}
8 F ti
7/31/2019 2 - PHP basic
60/76
www.smartosc.com
8. Functions
Create a PHP Function
A function will be executed by a call to the
function.
Syntax
functionfunctionName()
{
code to be executed;
}
8 F ti
7/31/2019 2 - PHP basic
61/76
www.smartosc.com
8. Functions
Create a PHP Function
A function will be executed by a call to the
function.
Syntax
functionfunctionName()
{
code to be executed;
}
8 F ti
7/31/2019 2 - PHP basic
62/76
www.smartosc.com
8. Functions
A simple function that writes my name when it
is called:
8 F ti
7/31/2019 2 - PHP basic
63/76
www.smartosc.com
8. Functions
PHP Functions - Adding parameters To add more functionality to a function, we can add parameters.
A parameter is just like
a variable.
Parameters are specified
after the function name,
inside the parentheses.
8 F nctions
7/31/2019 2 - PHP basic
64/76
www.smartosc.com
8. Functions
PHP Functions - Return values
To let a function return a value, use the return
statement.
III. PHP LANGUAGE
7/31/2019 2 - PHP basic
65/76
www.smartosc.com
CONCEPTS
1. References
2. Including Files
3. File System Basics
1 References
7/31/2019 2 - PHP basic
66/76
www.smartosc.com
1. References
A reference in PHP is simply a variable
corresponding to the same zval as anothervariable.
References can be explicitly created using a
special form of the assignment operator with anampersand after the equals sign.
For example:
1 References
7/31/2019 2 - PHP basic
67/76
www.smartosc.com
1. References
Passing Arguments by Reference Function arguments can also be passed by reference. This allows
your function to affect external variables:
2 Including Files
7/31/2019 2 - PHP basic
68/76
www.smartosc.com
2. Including Files
In PHP, you can insert the content of one PHP file into another
PHP file before the server executes it. The include and require statements are used to insert useful
codes written in other files, in the flow of execution.
2 Including Files
7/31/2019 2 - PHP basic
69/76
www.smartosc.com
2. Including Files
Include and require are identical, except upon failure:
require will produce a fatal error (E_COMPILE_ERROR) and stopthe script
include will only produce a warning (E_WARNING) and the script
will continue
So, if you want the execution to go on and show users theoutput, even ifthe include file is missing, use include.
Otherwise, in case ofFrameWork, CMS or a complex PHP
application coding, always use require to include a key file to
the flow of execution. This will help avoid compromising yourapplication's security and integrity, just in-case one key file is
accidentally missing.
3 File System Basics
7/31/2019 2 - PHP basic
70/76
www.smartosc.com
3. File System Basics
PHP provides several different ways to create, read from and
write to files, depending on the type of operation that you needto perform.
3 File System Basics
7/31/2019 2 - PHP basic
71/76
www.smartosc.com
3. File System Basics
Read a File
Provide the directory nameecho dirname( __FILE__ ) . "
";
Check this file exist within the directory?
file_exists( dirname( __FILE__ ) . "/basic.php" ) ? "yes" : "no";
3 File System Basics
7/31/2019 2 - PHP basic
72/76
www.smartosc.com
3. File System Basics
Write and Append to a File
3 File System Basics
7/31/2019 2 - PHP basic
73/76
www.smartosc.com
3. File System Basics
Check End-of-file
The feof() function checks if the "end-of-file" (EOF) has beenreached. The feof() function is useful for looping through data of
unknown length.
Note: You cannot read from files opened in w, a, and x mode!
if (feof($file)) echo "End of file";
The example below reads a file line by line, until the end of file is
reached:
3 File System Basics
7/31/2019 2 - PHP basic
74/76
www.smartosc.com
3. File System Basics
Reading a File Character by Character
The fgetc() function is used to read a single character from a file.Note: After a call to this function the file pointer moves to the
next character.
Example: The example below reads a file character by character,
until the end of file is reached:
7/31/2019 2 - PHP basic
75/76
END OF TOPIC
7/31/2019 2 - PHP basic
76/76
END OF TOPIC
Installing XAMPP on Windows Guidehttp://sawmac.com/xampp/
Zend PHP 5 Certification Study Guide book -http://www.mediafire.com/?ogi3rnpabbz6dhy
Easy to learn PHP http://w3schools.com
Find everything in PHP Manual
http://php.net/manual/en/index.php
Thank you for looking my side !
Made by SmartOsc - ManhNV
http://sawmac.com/xampp/http://www.mediafire.com/?ogi3rnpabbz6dhyhttp://w3schools.com/http://php.net/manual/en/index.phphttp://php.net/manual/en/index.phphttp://w3schools.com/http://www.mediafire.com/?ogi3rnpabbz6dhyhttp://sawmac.com/xampp/Top Related