PHP PHP HYPERTEXT HYPERTEXT PREPROCESSOR PREPROCESSOR
PHP Introduction PHP Introduction PHP is a recursive acronym for “PHP: PHP is a recursive acronym for “PHP: Hypertext Preprocessor” -- It is a widely- Hypertext Preprocessor” -- It is a widely- used open source general-purpose used open source general-purpose scripting language that is especially scripting language that is especially suited for web development and can be suited for web development and can be embedded into HTML. embedded into HTML.
PHP Introduction PHP Introduction
Server Side Programming Server Side Programming • Provides web site developers to Provides web site developers to utilise utilise resources on the web server resources on the web server • Non-public resources do not require direct Non-public resources do not require direct access from the clients access from the clients • Allows web sites to be client agnostic (unless Allows web sites to be client agnostic (unless JavaScript is used also) JavaScript is used also) • Most server side programming script is Most server side programming script is embedded within markup (although does not embedded within markup (although does not have to be, sometimes better not to) have to be, sometimes better not to)
PHP - What is it / does it do? PHP - What is it / does it do? • PHP: PHP: P PHP HP H Hypertext ypertext P Pre-processor re-processor • Programming language that is interpreted Programming language that is interpreted and executed on the server and executed on the server • Execution is done before delivering content Execution is done before delivering content to the client to the client • Contains a Contains a vast vast library of functionality that library of functionality that programmers can harness programmers can harness • Executes entirely on the server, requiring no Executes entirely on the server, requiring no specific features from the client specific features from the client
PHP - What is it / does it do? PHP - What is it / does it do? • Static resources such as regular HTML are simply output to Static resources such as regular HTML are simply output to the client from the server the client from the server • Dynamic resources such as PHP scripts are processed on the Dynamic resources such as PHP scripts are processed on the server prior to being output to the client server prior to being output to the client • PHP has the capability of connecting to many database PHP has the capability of connecting to many database systems making the entire process transparent to the client systems making the entire process transparent to the client User Web Server PHP Engine – Run Script Web Page Request Load PHP File PHP Results HTML Response
PHP Language Basics PHP Language Basics • Look at the building blocks of the PHP Look at the building blocks of the PHP language language – Syntax and structure Syntax and structure – Variables, constants and operators Variables, constants and operators – Data types and conversions Data types and conversions – Decision making IF and switch Decision making IF and switch – Interacting with the client application Interacting with the client application (HTML forms) (HTML forms)
PHP - Syntax and Structure PHP - Syntax and Structure • PHP is similar to C PHP is similar to C • All scripts start with <?php and with with ?> All scripts start with <?php and with with ?> • Line separator: ; (semi-colon) Line separator: ; (semi-colon) • Code block: { //code here } (brace brackets) Code block: { //code here } (brace brackets) • White space is generally ignored (not in strings) White space is generally ignored (not in strings) • Comments are created using: Comments are created using: – // single line quote // single line quote – /* Multiple line block quote */ /* Multiple line block quote */ • Precedence Precedence – Enforced using parentheses Enforced using parentheses – E.g. $sum = 5 + 3 * 6; // would equal 23 E.g. $sum = 5 + 3 * 6; // would equal 23 – $sum = (5 + 3) * 6; // would equal 48 $sum = (5 + 3) * 6; // would equal 48
PHP - Variables PHP - Variables • Prefixed with a $ Prefixed with a $ • Assign values with = operator Assign values with = operator • Example: $author = “Trevor Adams”; Example: $author = “Trevor Adams”; • No need to define type No need to define type • Variable names are Variable names are case sensitive case sensitive – $author and $Author are different $author and $Author are different
PHP - Constants PHP - Constants • Constants are special variables that Constants are special variables that cannot be changed cannot be changed • Use them for named items that will not Use them for named items that will not change change • Created using a define function Created using a define function – define(‘milestokm’, 1.6); define(‘milestokm’, 1.6); – Used without $ Used without $ – $km = 5 * milestokm; $km = 5 * milestokm;
PHP - Operators PHP - Operators • Standard mathematical operators Standard mathematical operators – +, -, *, / and % (modulus) +, -, *, / and % (modulus) • String concatenation with a period (.) String concatenation with a period (.) – $car = “SEAT” . “ Altea”; $car = “SEAT” . “ Altea”; – echo $car; would output “SEAT Altea” echo $car; would output “SEAT Altea” • Basic Boolean comparison with “==” Basic Boolean comparison with “==” – Using only = will overwrite a variable value Using only = will overwrite a variable value – Less than < and greater than > Less than < and greater than > – <= and >= as above but include equality <= and >= as above but include equality
PHP - Data Types PHP - Data Types • PHP is PHP is not not strictly typed strictly typed – Different to JAVA where all variables are declared Different to JAVA where all variables are declared • A data type is either text or numeric A data type is either text or numeric – PHP decides what type a variable is PHP decides what type a variable is – PHP can use variables in an appropriate way automatically PHP can use variables in an appropriate way automatically • E.g. E.g. – $vat_rate = 0.175; /* VAT Rate is numeric */ $vat_rate = 0.175; /* VAT Rate is numeric */ – echo $vat_rate * 100 . “%”; //outputs “17.5%” echo $vat_rate * 100 . “%”; //outputs “17.5%” – $vat_rate is converted to a string for the purpose of the echo $vat_rate is converted to a string for the purpose of the echo statement statement • Object, Array and unknown also exist as types, Be aware Object, Array and unknown also exist as types, Be aware of them but we shall not explore them today of them but we shall not explore them today
PHP Operators PHP Operators Operators are used to operate on values. Operators are used to operate on values. There are four classifications of operators: There are four classifications of operators: > > Arithmetic Arithmetic > > Assignment Assignment > > Comparison Comparison > > Logical Logical
PHP Conditional Statements PHP Conditional Statements •> > if if statement - use this statement to execute statement - use this statement to execute some code only if a specified condition is true some code only if a specified condition is true •> > if...else if...else statement - use this statement to statement - use this statement to execute some code if a condition is true and execute some code if a condition is true and another code if the condition is false another code if the condition is false •> > if...elseif....else if...elseif....else statement - use this statement statement - use this statement to select one of several blocks of code to be to select one of several blocks of code to be executed executed •> > switch switch statement - use this statement to select statement - use this statement to select one of many blocks of code to be executed one of many blocks of code to be executed
PHP Arrays PHP Arrays •> > An array variable is a storage area An array variable is a storage area holding a number or text. The problem is, holding a number or text. The problem is, a variable will hold only one value. a variable will hold only one value. •> > An array is a special variable, which can An array is a special variable, which can store multiple values in one single store multiple values in one single variable. variable.
PHP Arrays PHP Arrays • In PHP, there are three kind of arrays: In PHP, there are three kind of arrays: • > > Numeric array Numeric array - An array with a - An array with a numeric index numeric index • > > Associative array Associative array - An array where - An array where each ID key is associated with a value each ID key is associated with a value • > > Multidimensional array Multidimensional array - An array - An array containing one or more arrays containing one or more arrays
PHP Loops PHP Loops •> > while while - loops through a block of code while - loops through a block of code while a specified condition is true a specified condition is true •> > do...while do...while - loops through a block of code - loops through a block of code once, and then repeats the loop as long as a once, and then repeats the loop as long as a specified condition is true specified condition is true •> > for for - loops through a block of code a - loops through a block of code a specified number of times specified number of times •> > foreach foreach - loops through a block of code for - loops through a block of code for each element in an array each element in an array
PHP Loops - While PHP Loops - While The while loop executes a block of code The while loop executes a block of code while a condition is true. The example while a condition is true. The example below defines a loop that starts with below defines a loop that starts with i=1. The loop will i=1. The loop will continue to run as continue to run as long as i is less long as i is less than, or equal to 5. than, or equal to 5. i will increase by 1 i will increase by 1 each time the loop each time the loop runs: runs:
PHP Loops - While PHP Loops - While
PHP Loops – Do ... While PHP Loops – Do ... While The do...while statement will always The do...while statement will always execute the block of code once, it will then execute the block of code once, it will then check the condition, and repeat the loop check the condition, and repeat the loop while the condition is true. while the condition is true. The next example defines a loop that The next example defines a loop that starts with i=1. It will then increment i with starts with i=1. It will then increment i with 1, and write some output. Then the 1, and write some output. Then the condition is checked, and the loop will condition is checked, and the loop will continue to run as long as i is less than, or continue to run as long as i is less than, or equal to 5: equal to 5:
PHP Loops – Do ... While PHP Loops – Do ... While
PHP Loops - For PHP Loops - For • Parameters: Parameters: • > > init init: Mostly used to set a counter (but can : Mostly used to set a counter (but can be any code to be executed once at the be any code to be executed once at the beginning of the loop) beginning of the loop) • > > condition condition: Evaluated for each loop iteration. : Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. evaluates to FALSE, the loop ends. • > > increment increment: Mostly used to increment a : Mostly used to increment a counter (but can be any code to be executed counter (but can be any code to be executed at the end of the loop) at the end of the loop)
PHP Loops - For PHP Loops - For The example below defines a loop that The example below defines a loop that starts with i=1. The loop will continue to starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs: will increase by 1 each time the loop runs:
PHP Functions PHP Functions A function will be executed by a call to the A function will be executed by a call to the function. function. > > Give the function a name that reflects Give the function a name that reflects what the function does what the function does > > The function name can start with a letter The function name can start with a letter or underscore (not a number) or underscore (not a number)
PHP Forms - $_GET Function PHP Forms - $_GET Function •> > The built-in $_GET function is used to The built-in $_GET function is used to collect values from a form sent with collect values from a form sent with method="get". method="get". •> > Information sent from a form with the Information sent from a form with the GET method is visible to everyone (it will be GET method is visible to everyone (it will be displayed in the browser's address bar) and displayed in the browser's address bar) and has limits on the amount of information to has limits on the amount of information to send (max. 100 characters). send (max. 100 characters).
PHP Forms - $_GET Function PHP Forms - $_GET Function Notice how the URL carries the information after the file name.
PHP Forms - $_POST Function PHP Forms - $_POST Function •> > The built-in $_POST function is used to collect The built-in $_POST function is used to collect values from a form sent with method="post". values from a form sent with method="post". •> > Information sent from a form with the POST Information sent from a form with the POST method is invisible to others and has no limits on method is invisible to others and has no limits on the amount of information to send. the amount of information to send. •> > Note: However, there is an 8 Mb max size for Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by the POST method, by default (can be changed by setting the post_max_size in the php.ini file). setting the post_max_size in the php.ini file).
PHP Forms - $_POST Function PHP Forms - $_POST Function And here is what the code of action.php might look like:

php basic notes of concept for beginners

  • 1.
  • 2.
    PHP Introduction PHP Introduction PHPis a recursive acronym for “PHP: PHP is a recursive acronym for “PHP: Hypertext Preprocessor” -- It is a widely- Hypertext Preprocessor” -- It is a widely- used open source general-purpose used open source general-purpose scripting language that is especially scripting language that is especially suited for web development and can be suited for web development and can be embedded into HTML. embedded into HTML.
  • 3.
  • 4.
    Server Side Programming ServerSide Programming • Provides web site developers to Provides web site developers to utilise utilise resources on the web server resources on the web server • Non-public resources do not require direct Non-public resources do not require direct access from the clients access from the clients • Allows web sites to be client agnostic (unless Allows web sites to be client agnostic (unless JavaScript is used also) JavaScript is used also) • Most server side programming script is Most server side programming script is embedded within markup (although does not embedded within markup (although does not have to be, sometimes better not to) have to be, sometimes better not to)
  • 5.
    PHP - Whatis it / does it do? PHP - What is it / does it do? • PHP: PHP: P PHP HP H Hypertext ypertext P Pre-processor re-processor • Programming language that is interpreted Programming language that is interpreted and executed on the server and executed on the server • Execution is done before delivering content Execution is done before delivering content to the client to the client • Contains a Contains a vast vast library of functionality that library of functionality that programmers can harness programmers can harness • Executes entirely on the server, requiring no Executes entirely on the server, requiring no specific features from the client specific features from the client
  • 6.
    PHP - Whatis it / does it do? PHP - What is it / does it do? • Static resources such as regular HTML are simply output to Static resources such as regular HTML are simply output to the client from the server the client from the server • Dynamic resources such as PHP scripts are processed on the Dynamic resources such as PHP scripts are processed on the server prior to being output to the client server prior to being output to the client • PHP has the capability of connecting to many database PHP has the capability of connecting to many database systems making the entire process transparent to the client systems making the entire process transparent to the client User Web Server PHP Engine – Run Script Web Page Request Load PHP File PHP Results HTML Response
  • 7.
    PHP Language Basics PHPLanguage Basics • Look at the building blocks of the PHP Look at the building blocks of the PHP language language – Syntax and structure Syntax and structure – Variables, constants and operators Variables, constants and operators – Data types and conversions Data types and conversions – Decision making IF and switch Decision making IF and switch – Interacting with the client application Interacting with the client application (HTML forms) (HTML forms)
  • 8.
    PHP - Syntaxand Structure PHP - Syntax and Structure • PHP is similar to C PHP is similar to C • All scripts start with <?php and with with ?> All scripts start with <?php and with with ?> • Line separator: ; (semi-colon) Line separator: ; (semi-colon) • Code block: { //code here } (brace brackets) Code block: { //code here } (brace brackets) • White space is generally ignored (not in strings) White space is generally ignored (not in strings) • Comments are created using: Comments are created using: – // single line quote // single line quote – /* Multiple line block quote */ /* Multiple line block quote */ • Precedence Precedence – Enforced using parentheses Enforced using parentheses – E.g. $sum = 5 + 3 * 6; // would equal 23 E.g. $sum = 5 + 3 * 6; // would equal 23 – $sum = (5 + 3) * 6; // would equal 48 $sum = (5 + 3) * 6; // would equal 48
  • 9.
    PHP - Variables PHP- Variables • Prefixed with a $ Prefixed with a $ • Assign values with = operator Assign values with = operator • Example: $author = “Trevor Adams”; Example: $author = “Trevor Adams”; • No need to define type No need to define type • Variable names are Variable names are case sensitive case sensitive – $author and $Author are different $author and $Author are different
  • 10.
    PHP - Constants PHP- Constants • Constants are special variables that Constants are special variables that cannot be changed cannot be changed • Use them for named items that will not Use them for named items that will not change change • Created using a define function Created using a define function – define(‘milestokm’, 1.6); define(‘milestokm’, 1.6); – Used without $ Used without $ – $km = 5 * milestokm; $km = 5 * milestokm;
  • 11.
    PHP - Operators PHP- Operators • Standard mathematical operators Standard mathematical operators – +, -, *, / and % (modulus) +, -, *, / and % (modulus) • String concatenation with a period (.) String concatenation with a period (.) – $car = “SEAT” . “ Altea”; $car = “SEAT” . “ Altea”; – echo $car; would output “SEAT Altea” echo $car; would output “SEAT Altea” • Basic Boolean comparison with “==” Basic Boolean comparison with “==” – Using only = will overwrite a variable value Using only = will overwrite a variable value – Less than < and greater than > Less than < and greater than > – <= and >= as above but include equality <= and >= as above but include equality
  • 12.
    PHP - DataTypes PHP - Data Types • PHP is PHP is not not strictly typed strictly typed – Different to JAVA where all variables are declared Different to JAVA where all variables are declared • A data type is either text or numeric A data type is either text or numeric – PHP decides what type a variable is PHP decides what type a variable is – PHP can use variables in an appropriate way automatically PHP can use variables in an appropriate way automatically • E.g. E.g. – $vat_rate = 0.175; /* VAT Rate is numeric */ $vat_rate = 0.175; /* VAT Rate is numeric */ – echo $vat_rate * 100 . “%”; //outputs “17.5%” echo $vat_rate * 100 . “%”; //outputs “17.5%” – $vat_rate is converted to a string for the purpose of the echo $vat_rate is converted to a string for the purpose of the echo statement statement • Object, Array and unknown also exist as types, Be aware Object, Array and unknown also exist as types, Be aware of them but we shall not explore them today of them but we shall not explore them today
  • 13.
    PHP Operators PHP Operators Operatorsare used to operate on values. Operators are used to operate on values. There are four classifications of operators: There are four classifications of operators: > > Arithmetic Arithmetic > > Assignment Assignment > > Comparison Comparison > > Logical Logical
  • 14.
    PHP Conditional Statements PHPConditional Statements •> > if if statement - use this statement to execute statement - use this statement to execute some code only if a specified condition is true some code only if a specified condition is true •> > if...else if...else statement - use this statement to statement - use this statement to execute some code if a condition is true and execute some code if a condition is true and another code if the condition is false another code if the condition is false •> > if...elseif....else if...elseif....else statement - use this statement statement - use this statement to select one of several blocks of code to be to select one of several blocks of code to be executed executed •> > switch switch statement - use this statement to select statement - use this statement to select one of many blocks of code to be executed one of many blocks of code to be executed
  • 15.
    PHP Arrays PHP Arrays •> >An array variable is a storage area An array variable is a storage area holding a number or text. The problem is, holding a number or text. The problem is, a variable will hold only one value. a variable will hold only one value. •> > An array is a special variable, which can An array is a special variable, which can store multiple values in one single store multiple values in one single variable. variable.
  • 16.
    PHP Arrays PHP Arrays •In PHP, there are three kind of arrays: In PHP, there are three kind of arrays: • > > Numeric array Numeric array - An array with a - An array with a numeric index numeric index • > > Associative array Associative array - An array where - An array where each ID key is associated with a value each ID key is associated with a value • > > Multidimensional array Multidimensional array - An array - An array containing one or more arrays containing one or more arrays
  • 17.
    PHP Loops PHP Loops •> >while while - loops through a block of code while - loops through a block of code while a specified condition is true a specified condition is true •> > do...while do...while - loops through a block of code - loops through a block of code once, and then repeats the loop as long as a once, and then repeats the loop as long as a specified condition is true specified condition is true •> > for for - loops through a block of code a - loops through a block of code a specified number of times specified number of times •> > foreach foreach - loops through a block of code for - loops through a block of code for each element in an array each element in an array
  • 18.
    PHP Loops -While PHP Loops - While The while loop executes a block of code The while loop executes a block of code while a condition is true. The example while a condition is true. The example below defines a loop that starts with below defines a loop that starts with i=1. The loop will i=1. The loop will continue to run as continue to run as long as i is less long as i is less than, or equal to 5. than, or equal to 5. i will increase by 1 i will increase by 1 each time the loop each time the loop runs: runs:
  • 19.
    PHP Loops -While PHP Loops - While
  • 20.
    PHP Loops –Do ... While PHP Loops – Do ... While The do...while statement will always The do...while statement will always execute the block of code once, it will then execute the block of code once, it will then check the condition, and repeat the loop check the condition, and repeat the loop while the condition is true. while the condition is true. The next example defines a loop that The next example defines a loop that starts with i=1. It will then increment i with starts with i=1. It will then increment i with 1, and write some output. Then the 1, and write some output. Then the condition is checked, and the loop will condition is checked, and the loop will continue to run as long as i is less than, or continue to run as long as i is less than, or equal to 5: equal to 5:
  • 21.
    PHP Loops –Do ... While PHP Loops – Do ... While
  • 22.
    PHP Loops -For PHP Loops - For • Parameters: Parameters: • > > init init: Mostly used to set a counter (but can : Mostly used to set a counter (but can be any code to be executed once at the be any code to be executed once at the beginning of the loop) beginning of the loop) • > > condition condition: Evaluated for each loop iteration. : Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. evaluates to FALSE, the loop ends. • > > increment increment: Mostly used to increment a : Mostly used to increment a counter (but can be any code to be executed counter (but can be any code to be executed at the end of the loop) at the end of the loop)
  • 23.
    PHP Loops -For PHP Loops - For The example below defines a loop that The example below defines a loop that starts with i=1. The loop will continue to starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. i run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs: will increase by 1 each time the loop runs:
  • 24.
    PHP Functions PHP Functions Afunction will be executed by a call to the A function will be executed by a call to the function. function. > > Give the function a name that reflects Give the function a name that reflects what the function does what the function does > > The function name can start with a letter The function name can start with a letter or underscore (not a number) or underscore (not a number)
  • 25.
    PHP Forms -$_GET Function PHP Forms - $_GET Function •> > The built-in $_GET function is used to The built-in $_GET function is used to collect values from a form sent with collect values from a form sent with method="get". method="get". •> > Information sent from a form with the Information sent from a form with the GET method is visible to everyone (it will be GET method is visible to everyone (it will be displayed in the browser's address bar) and displayed in the browser's address bar) and has limits on the amount of information to has limits on the amount of information to send (max. 100 characters). send (max. 100 characters).
  • 26.
    PHP Forms -$_GET Function PHP Forms - $_GET Function Notice how the URL carries the information after the file name.
  • 27.
    PHP Forms -$_POST Function PHP Forms - $_POST Function •> > The built-in $_POST function is used to collect The built-in $_POST function is used to collect values from a form sent with method="post". values from a form sent with method="post". •> > Information sent from a form with the POST Information sent from a form with the POST method is invisible to others and has no limits on method is invisible to others and has no limits on the amount of information to send. the amount of information to send. •> > Note: However, there is an 8 Mb max size for Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by the POST method, by default (can be changed by setting the post_max_size in the php.ini file). setting the post_max_size in the php.ini file).
  • 28.
    PHP Forms -$_POST Function PHP Forms - $_POST Function And here is what the code of action.php might look like: