>> Fundamental Concepts in PHP
Variables Variable In PHP An entity that can hold a value Variable Names start with “$” sign Assignment operator is the same “=“ Must begin with a letter or an underscore (_) Must contain only alpha-numeric values and underscore No Spaces Case-sensitive NAMING RULES TRY NOW Create a variable called name and add your name as the value
Strings Strings PHP Like JavaScript, PHP also uses strings String concatenation is done using “.” (unlike JavaScript where we use the “+” operator) Provides a number of string processing functions echo $txt1 . " " . $txt2; http://devdocs.io/php-string/
Greater than or equal to Operators Inc / Dec Operator Name ++ x Pre-increment x ++ Post-increment -- x Pre-decrement x -- Post-decrement Comparison Arithmetic Operator Name x + y Addition x - y Subtraction x * y Multiplication x / y Division x % y Modulus Operator Name x == y Equal x === y Identical x != y Not equal x <> y x !== y Not identical x > y Greater than x < y Less than x >= y Greater than or equal to x <= y Less than or equal to Logical Operator Name x and y And x or y Or x xor y Xor x && y x || y ! x Not
Conditional Conditionals PHP Provides a way to have certain commands executed only if some condition is met PHP If statement If…..Else statement Switch statement
IF Conditional If statement TRY NOW SYNTAX Create a variable password { } SYNTAX Create a variable password Initialize it with a value “p@ssw0rd” Now check if the password is equal to “p@ssw0rd” If true, Echo “Welcome” TRY NOW
IF-Else Conditional If-Else statement TRY NOW SYNTAX if(cond) { } else SYNTAX Now add an else statement to the previous statement If the password is not correct then echo “You are in the wrong neighborhood” TRY NOW
Complex Conditional If-Else statement with Complex Conditional TRY NOW if(cond) { } else SYNTAX Now add another variable called username and initialize to a value called “labuser”. Now change the if condition to check if username is equal to “labuser” and password is equal to “p@ssw0rd” TRY NOW
Switch Conditional Switch statement TRY NOW SYNTAX switch (n) { case label1: code to be executed if n=label1; break; ……… default: } SYNTAX TRY NOW Create and initialize a variable called item. Then switch the variable If item is eggs then echo “SAR 3.65” If item is milk then echo “SAR 5.00” Else echo out “SAR 0.00”
Loops Looping Provides a way to have certain block of commands to be executed again and again for certain number of times PHP While Loop Do….While Loop For Loop ForEach
While Loop While statement TRY NOW SYNTAX while(cond) { } SYNTAX Use the while loop to repeat some text 10 times TRY NOW
Do-While Loop Do-While statement At least runs the loop once TRY NOW { } while(cond); SYNTAX At least runs the loop once Repeat the previous exercise using do-while loops instead. TRY NOW
For Loop For Loop TRY NOW HINT: SYNTAX Create an array for(init; cond; increment) { } SYNTAX Create an array Display its elements using the for-loop TRY NOW HINT: Use count() to get the length of the array
Functions in PHP Functions Group Block of Code Reusable Code TRY NOW function name(paramters) { return value; } SYNTAX Separate by comma Use “$” sign Create a function add() which takes two parameters and returns the result which is the sum of the two numbers. TRY NOW
Important String Functions Length: strlen(‘string’); Substring: mb_substr(‘string’, start, length); Break string into array: explode(break-char, ‘string’); Join: implode(array-name, join-char); Reverse: strrev(‘string’); To Upper Case: strtoupper(‘string’); To Lower Case: strtolower(‘STRING’); Times Occurance: substr_count(‘My My’,’sub-string’);
Web-Based Systems - Misbhauddin Next on PHP Integrating PHP with HTML Reusing PHP Files Web-Based Systems - Misbhauddin
Web-based Systems | Misbhauddin Mini-PHP Project Mini Course Project Build an e-commerce website “Buy 4m Me” Listing all items you want to sell through the online store Customers can view and select the items Customers can add them to their cart Customer can checkout and make payment Confirmation Emails Web-based Systems | Misbhauddin