🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

PHP Basic Fundamentals.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

PHP BASICS ï‚¢ ï‚¢ ï‚¢ ï‚¢ ï‚¢ ï‚¢ ï‚¢ ï‚¢ DataType Variable Constant Operators Control and Looping structure Arrays PHP Errors Include vs Require By www.Creativedev.in DATATYPE A Data type refer to the type of data a variable can store. A Data type is determined at runtime by PHP. If you assign a string to a v...

PHP BASICS         DataType Variable Constant Operators Control and Looping structure Arrays PHP Errors Include vs Require By www.Creativedev.in DATATYPE A Data type refer to the type of data a variable can store. A Data type is determined at runtime by PHP. If you assign a string to a variable, it becomes a string variable and if you assign an integer value, the variable becomes an integer variable. CONTINUE... PHP supports eight primitive types: Four scalar types: Boolean 2. integer 3. float (floating-point number, aka double) 4. string Two compound types: 1. array 6. object And finally three special types: 5. 7. 8. 9. resource NULL callable CONTINUE... Integer : used to specify numeric value SYNTAX: <?php $variable = 10; ?> Float : used to specify real numbers SYNTAX: <?php $variable = -10; $variable1 = -10.5; ?> Boolean: values true or false, also 0 or empty. SYNTAX: <?php $variable = true; ?> CONTINUE... String: sequence of characters included in a single or double quote. SYNTAX: <?php $string = 'Hello World'; $string1 = "Hello\n World"; ?> VARIABLE   Variables are used for storing a values, like text strings, numbers or arrays. All variables in PHP start with a $ symbol. SYNTAX: $var_name = value; Example: $name = 'Bhumi'; VARIABLE SCOPE Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types: 1) Local variables 2) Function parameters 3) Global variables 4) Static variables VARIABLE NAMING RULES 1) 2) 3) A variable name must start with a letter or an underscore "_". Ex, $1var_name is not valid. Variable names are case sensitive; this means $var_name is different from $VAR_NAME. A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore. Ex, $var name is not valid,use $var_name VARIABLE VARIABLES Variable variables allow you to access the contents of a variable without knowing its name directly - it is like indirectly referring to a variable EXAMPLE: $a = 'hello'; $$a = 'world'; echo "$a $hello"; VARIABLE TYPE CASTING Type casting is converting a variable or value into a desired data type. This is very useful when performing arithmetic computations that require variables to be of the same data type. Type casting in PHP is done by the interpreter. PHP also allows you to cast the data type. This is known as Explicit casting. The code below demonstrates explicit type casting. VARIABLE TYPE CASTING EXAMPLE <?php $a = 1; $b = 1.5; $c = $a + $b; $c = $a + (int) $b; echo $c; ?> CONSTANTS  A constant is an identifier (name) for an unchangable value.  A valid constant name starts with a letter or underscore (no $ sign before the constant name). Note: Unlike variables, constants are automatically global across the entire application.Constants are usually In UPPERCASE. SYNTAX: define(name, value, case-insensitive) Parameters: name: Specifies the name of the constant value: Specifies the value of the constant case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false CONSTANT EXAMPLES <?php define('NAME','Bhumi'); echo NAME; ?> PHP OPERATORS 1. Arithmetic Operators OPERATOR DESCRIPTION EXAMPLE RESULT + Addition x=2,x+2 4 - Subtraction x=2,5-x 3 * Multiplication x=4,x*5 20 / Division 15/5,5/2 % ++ Modulus(division remainder) Increment x=5,x++ 3 2.5 1 2 6 -- Decrement x=5,x-- 4 5%2,10%8 PHP OPERATORS 2. Assignment Operators OPERATOR EXAMPLE IS THE SAME AS = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y .= x.=y x=x.y %= X%=y x=x%y PHP OPERATORS... 3. Logical Operators OPERATOR DESCRIPTION EXAMPLE && and x=6,y=3 (x < 10 && y > 1) return true || OR x=6,y=3 (x==5 || y==5) return false ! not x=6,y=3 !(x==y) return true PHP OPERATORS 4. Comparison Operators OPERATOR DESCRIPTION EXAMPLE == is equal to 5==8 return false != is not equal 5!=8 return true <> is not equal 5<>8 return true > is greater than 5>8 return false < is less than 5<8 return true >= is greater than or equal to 5>=8 return false <= is less than or equal to 5<=8 return true CONTINUE... 5. Ternary Operator ? is represent the ternary operator SYNTAX: (expr) ? if_expr_true : if_expr_false; expression evaluates TRUE or FALSE TRUE: first result (before colon) FALSE: second one (after colon) CONTINUE Ternary Operator Example <?php $a = 10; $b = 13; echo $a < $b ? 'Yes' : 'No'; ?> CONTROL AND LOOPING STRUCTURE 1. If/else 2. Switch 3. While 4. For loop 5. Foreach IF/ELSE STATEMENT Here is the example to use if/else statement <?php // change message depending on whether // number is less than zero or not $number = -88; if ($number < 0) { echo 'That number is negative'; } else { echo 'That number is either positive or zero'; } ?> THE IF-ELSEIF-ELSE STATEMENT <?php // handle multiple possibilities if($answer == ‘y’) { print "The answer was yes\n"; else if ($answer == ‘n’) { print "The answer was no\n"; }else{ print "Error: $answer is not a valid answer\n"; } ?> THE SWITCH-CASE STATEMENT <?php // handle multiple possibilities switch ($answer) { case 'y': print "The answer was yes\n"; break; case 'n': print "The answer was no\n"; break; default: } ?> print "Error: $answer is not a valid answer\n"; break; WHILE LOOP SYNTAX while (condition): code to be executed; endwhile; EXAMPLE <?php // repeats until counter becomes 10 $counter = 1; while($counter < 10){ echo $counter; $counter++; } ?> THE DO-WHILE LOOP SYNTAX: do { code to be executed; } while (condition); EXAMPLE <?php // repeats until counter becomes 10 $counter = 1; do{ echo $counter; $counter++; }while($counter < 10); ?> THE FOR LOOP SYNTAX for (init; cond; incr) { code to be executed; } init: Is mostly used to set a counter, but can be any code to be executed once at the beginning of the loop statement. cond: Is evaluated at beginning of each loop iteration. If the condition evaluates to TRUE, the loop continues and the code executes. If it evaluates to FALSE, the execution of the loop ends. incr: Is mostly used to increment a counter, but can be any code to be executed at the end of each loop. BREAKING A LOOP Occasionally it is necessary to exit from a loop before it has met whatever completion criteria were specified. To achieve this, the break statement must be used. The following example contains a loop that uses the break statement to exit from the loop when i = 100, even though the loop is designed to iterate 100 times: for ($i = 0; $i < 100; $i++) { if ($i == 10) { break; } } CONTINUE Skipping Statements in Current Loop Iteration. continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. for ($i = 0; $i < 5; ++$i) { if ($i == 2) continue print "$i\n"; } EXAMPLE: <?php // repeat continuously until counter becomes 10 // output: for ($x=1; $x<10; $x++) { echo "$x "; } ?> ARRAY IN PHP An array can store one or more values in a single variable name. There are three different kind of arrays: 1. Numeric array - An array with a numeric ID key 2. Associative array - An array where each ID key is associated with a value 3. Multidimensional array - An array containing one or more arrays FOREACH LOOP SYNTAX: foreach (array as value) { code to be executed; } <?php $fruits = array( 'a' => 'apple', 'b' => 'banana', 'p' => 'pineapple', 'g' => 'grape'); ?> FOREACH LOOP $array = ['apple', 'banana', 'orange‘]; foreach ($array as $value) { $array = strtoupper($value); } foreach ($array as $key => $value) { $array[$key] = strtolower($value); } var_dump($array); array(3) { [0]=> string(3) "APPLE" [1]=> string(3) "BANANA" [2]=> &string(3) "ORANGE" } ERRORS AND ERROR MANAGEMENT Errors: Basically errors can be of one of two types External Errors Logic Errors (Bugs) What about these error types? External Errors will always occur at some point or another External Errors which are not accounted for are Logic Errors Logic Errors are harder to track down PHP ERRORS Four levels of error condition to start with • Strict standard problems (E_STRICT) • Notices (E_NOTICE) • Warnings (E_WARNING) • Errors (E_ERROR) To enable error in PHP : ini_set('display_errors', 1); error_reporting(E_ALL); PHP ERRORS EXAMPLE // E_NOTICE : <?php echo $x = $y + 3; ?> Notice: Undefined variable: y // E_WARNING <?php $fp = fopen('test_file', 'r'); ?> Warning: fopen(test_file): failed to open stream: No such file or directory // E_ERROR <?php NonFunction(); ?> Fatal error: Call to undefined function NonFunction() REQUIRE, INCLUDE 1. 2. 3. require('filename.php');  Include and evaluate the specified file  Fatal Error include('filename.php');  Include and evaluate the specified file  Warning require_once/include_once  If already included,won't be include again

Use Quizgecko on...
Browser
Browser