0% found this document useful (0 votes)
33 views28 pages

Itec106 04 PHP

PHP is a widely-used open source scripting language that can be embedded into HTML. PHP code is executed on the server and generates HTML that is sent to the browser. PHP supports variables, functions, arrays, conditionals, loops, and object-oriented programming. It also interfaces with databases like MySQL to perform CRUD operations and retrieve data for use in scripts.

Uploaded by

Lizette Paez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views28 pages

Itec106 04 PHP

PHP is a widely-used open source scripting language that can be embedded into HTML. PHP code is executed on the server and generates HTML that is sent to the browser. PHP supports variables, functions, arrays, conditionals, loops, and object-oriented programming. It also interfaces with databases like MySQL to perform CRUD operations and retrieve data for use in scripts.

Uploaded by

Lizette Paez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

PHP

ITEC106
What is PHP?
• PHP was conceived sometime in the fall off 1994 by Rasmus Lerdof. Early
non-released versions were used on his home page to keep track of who
was looking at his online resume.
• The first version used by others was available sometime in early 1995 and
was known as the Personal Home Page Tools.
• PHP is an acronym for "PHP: Hypertext Preprocessor"
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server
• PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
• You can also write PHP code in an object-oriented style.
Basic PHP Syntax
• A PHP script is executed on the server, and the plain HTML result is
sent back to the browser.
• A PHP script can be placed anywhere in the document.
• A PHP script starts with <?php and ends with ?>
• The default file extension for PHP files is ".php".
• A PHP file normally contains HTML tags, and some PHP scripting code.
Creating (Declaring) PHP Variables
• In PHP, a variable starts with the $ sign, followed by the name of the
variable
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)
• PHP variable names are case-sensitive!
Output Variables
• The PHP echo statement is often used to output data to the screen.
• The following example will show how to output text and a variable:
PHP The global Keyword
• The global keyword is used to
access a global variable from
within a function.
• To do this, use the global
keyword before the variables
(inside the function):
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
• Object
• NULL
• Resource
PHP Operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
PHP String Operators
• PHP has two operators that are specially designed for strings.
PHP Array Operators
• The PHP array operators are used to compare arrays.
PHP Conditional Assignment Operators
• The PHP conditional assignment operators are used to set a value
depending on conditions:
PHP Conditional Statements
Very often when you write code, you want to perform different actions for
different conditions. You can use conditional statements in your code to do
this.
In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another
code if that condition is false
• if...elseif...else statement - executes different codes for more than two
conditions
• switch statement - selects one of many blocks of code to be executed
PHP Loops
Often when you write code, you want the same block of code to run over and over
again a certain number of times. So, instead of adding several almost equal code-
lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a
certain condition is true.
In PHP, we have the following loop types:
• while - loops through a block of code as long as the specified condition is true
• do...while - loops through a block of code once, and then repeats the loop as long
as the 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
PHP foreach Loop
• The foreach loop - Loops through a block of code for each element in
an array.
PHP Functions
• PHP Built-in Functions
• PHP has over 1000 built-in functions that can be called directly, from within a
script, to perform a specific task.
• PHP User Defined Functions
• A function is a block of statements that can be used repeatedly in a program.
• A function will not execute automatically when a page loads.
• A function will be executed by a call to the function.
PHP Arrays
• In PHP, the array() function is used to create an array:
• In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays
PHP Indexed Arrays
PHP Associative Arrays
PHP Multidimensional Arrays
Sort Functions For Arrays
• sort() - sort arrays in ascending order
• rsort() - sort arrays in descending order
• asort() - sort associative arrays in ascending order, according to the
value
• ksort() - sort associative arrays in ascending order, according to the key
• arsort() - sort associative arrays in descending order, according to the
value
• krsort() - sort associative arrays in descending order, according to the
key
PHP Global Variables - Superglobals
Some predefined variables in PHP are "superglobals", which means that they are always
accessible, regardless of scope - and you can access them from any function, class or file without
having to do anything special.
• $GLOBALS
• $_SERVER
• $_REQUEST
• $_POST
• $_GET
• $_FILES
• $_ENV
• $_COOKIE
• $_SESSION
PHP Connect to MySQL
PHP MySQL Insert Data
Select Data From a MySQL Database
PHP MySQL Delete Data
PHP MySQL Update Data
References
• https://www.w3schools.com/php/php_math.asp
• https://www.w3schools.com/php/php_superglobals.html
• https://www.w3schools.com/php/php_oop_what_is.html

You might also like