Chapter 4 Introduction to PHP Lectured by: Nguyễn Hữu Hiếu PHP n PHP is a server scripting language, and is a powerful tool for making dynamic and interactive Web pages quickly n PHP (recursive acronym for PHP: Hypertext Preprocessor) n PHP is a widely-used, and free. n PHP runs over different operating systems such as Windows, Linux, Mac Os and Unix. n PHP scripts are executed on the server, and the plain HTML result is sent back to the browser. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 2 © 2017 About the PHP Language • Syntax is inspired by C • Curly braces, semicolons, no signficant whitespace • Syntax inspired by perl • Dollar signs to start variable names, associative arrays • Extends HTML to add segments of PHP within an HTML file.
Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 3 © 2017 What Can PHP Do? n PHP can generate dynamic page content n PHP can create, open, read, write, delete, and close files on the server. n PHP can collect form data. n PHP can send and receive cookies. n PHP can add, delete, modify data in your database.
n PHP can restrict users to access some pages on your website. n PHP can encrypt data. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 4 © 2017 Introduction • Documents end with the extension .php • To trigger the PHP commands, you need <?php tag. and they finish only when the closing part ?> is encountered Example: <?php echo "Hello world"; ?> Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 5 © 2017 Example <h1>Hello from Dr.
Chuck's HTML Page</h1> <p> <?php echo "Hi there.\n"; $answer = 6 * 7; echo "The answer is $answer, what "; echo "was the question again?\n"; ?> </p> <p>Yes another paragraph.</p> Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 6 © 2017 Key Words abstract and array() as break case catch class clone const continue declare default do else elseif end declare endfor endforeach endif endswitch endwhile extends final for foreach function global goto if implements interface instanceof namespace new or private protected public static switch $this throw try use var while xor http://php.net/manual/en/reserved.php Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 7 © 2017 Variable Names • Start with a dollar sign ($) followed by a letter or underscore, followed by any number of letters, numbers, or underscores • Case matters $abc = 12; abc = 12; $total = 0; $2php = 0; $largest_so_far = 0; $bad-punc = 0; http://php.net/manual/en/language.php Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 8 © 2017 Variable naming rules • Variable names must start with a letter of the alphabet or the _ (underscore) character. • Variable names can contain only the characters a-z, A-Z, 0-9, and _ (underscore). • Variable names may not contain spaces. If a variable must comprise more than one word, the words should be separated with the _ (underscore) character (e.
• Variable names are case-sensitive. The variable $High_Score is not the same as the variable $high_score. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 9 © 2017 Variable Name Weirdness • Things that look like variables but are missing a dollar sign can be confusing $x = 2; $x = 2; $y = x + 5; y = $x + 5; print $y; print $x; 5 Parse error Parse error: syntax error, unexpected '=' Print $x // 2 in C:\xampp\htdocs\vtes.php on line 2 Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 10 © 2017 Expressions • Completely normal like other languages ( + - / * ) • More agressive implicit type conversion <?php $x = "15" + 27; echo($x); 42 echo("\n"); ?> Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 11 © 2017 Arithmetic operators • They are used to perform mathematics. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 12 © 2017 Assignment operators • These operators are used to assign values to variables.
• Strings have their own operator, the period (.), detailed in the section “String concatenation” Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 13 © 2017 Comparison operators • Comparison operators are generally used inside a construct such as an if statement in which you need to compare two items. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 14 © 2017 Logical operators • For example, you might say to yourself, “If the time is later than 12 PM and earlier than 2 PM, then have lunch.” In PHP, • if ($hour > 12 && $hour < 14) dolunch(); Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 15 © 2017 Variable Assignment ¡ The syntax to assign a value to a variable is always: variable = value Ex. $x += 10; ¡ Variable incrementing and decrementing ¡ Adding or subtracting 1 operation ¡ Prefix form: ++$x; --$y; Ex: if (++$x == 10) echo $x; ¡ This tells PHP to first increment the value of $x and then test whether it has the value 10 and, if so, output its value. ¡ Postfix form: ++$x; --$y; Ex: if ($y−− == 0) echo $y; ¡ Suppose $y starts out as 0 before the statement is executed.
The comparison will return a TRUE result, but $y will be set to −1 after the comparison is made. So what will the echo statement display: 0 or −1? Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 16 © 2017 Variable Typing ¡ In php, variables do not have to be declared before they are used. ¡ PHP always converts variables to the type required by their context when they are accessed. ¡ you can create a multiple-digit number and extract the nth digit from it, simply by assuming it to be a string.
<?php $number = 12345 * 67890; //=838102050 echo substr($number, 3, 1);//(number,position, no.of char) ?> ¡ $number is a numeric variable. ¡ But the PHP function substr asks for one character to be returned from $number ¡ PHP turns $number into a nine-character string. ¡ so that substr can access it and return the character, which in this case is 1. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 17 © 2017 Cont.
n The same goes for turning a string into a number n The variable $pi is set to a string value automatically turned into a floating-point in the third line by the equation for calculating a circle’s area n Example: Automatically converting a string to a number <?php $pi = "3.1415927"; $radius = 5; echo $pi * ($radius * $radius); ?> Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 18 © 2017 Output • echo is a language construct - <?php can be treated like a function $x = "15" + 27; with one parameter. Without echo $x; parenthesis, it accepts multiple echo("\n"); parameters. echo $x, "\n"; • print is a function - only one print $x; print "\n"; parameter but parenthesis are print($x); optional so it can look like a print("\n"); ?> language construct Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 19 © 2017 Echo Commands • echo command used in a number of different ways to output text from the server to your browser. • In some cases, a string literal has been output.
echo “welcome in php”; • In others, strings have first been concatenated. $x=7; echo “the number is = “.$x; • or variables have been evaluated. $x=7; echo “the number is = $x“; • It was Also shown the output spread over multiple lines. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 20 © 2017 The Difference Between the echo and print Commands n an alternative to echo that you can use: print.
n The two commands are quite similar to each other, but print is an actual function that takes a single parameter, whereas echo is a PHP language construct. n Echo is faster than print in general text output, because is not being a function—it doesn’t set a return value. Example: $b=7; $a=1; $b ? print "TRUE" : print "FALSE";// TRUE $a>=$b ? print "TRUE" : print "FALSE";// FALSE Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 21 © 2017 Conditional - if • Logical operators ( == != < > <= >= && || !) • Curly braces \ <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { Hello World! print "Wrong answer\n"; } ?> Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 22 © 2017 Whitespace does not matter <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> <?php $ans = 42; if ( $ans == 42 ) { print "Hello world!\n"; } else { print "Wrong answer\n"; } ?> Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 23 © 2017 What Style do You Prefer? <?php $ans = 42; <?php if ( $ans == 42 ) $ans = 42; { if ( $ans == 42 ) { print "Hello world!\n"; print "Hello world!\n"; } } else { else print "Wrong answer\n"; { } print "Wrong answer\n"; ?> } ?> Aesthetics Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 24 © 2017 Associative Arrays • Like Python Dictonaries+Lists - but more powerful • Can be key => value or simply indexed by numbers • Ignore two-dimensional arrays for now. Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 25 © 2017 Integer Indices <?php $stuff = array("Hi", "There"); echo $stuff[1], "\n"; ?> There Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 26 © 2017 Integer Indices <?php $stuff = array(); $stuff[] = "Hello"; $stuff[] = "World"; echo $stuff[1], "\n"; ?> World Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 27 © 2017 Integer Indices <?php $stuff = array(); $stuff[2] = "Hello"; $stuff[9] = "World"; echo $stuff[9], "\n"; ?> World Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 28 © 2017 Key / Value <?php $stuff = array("name" => "Chuck", "course" => "SI664"); echo $stuff["course"], "\n"; ?> SI664 Trường Đại Học Bách Khoa TP.HCM Lập Trình Web Khoa Khoa Học và Kỹ Thuật Máy Tính 29 © 2017 Dumping an Array • The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = array("name" => "Chuck", "course" => "SI664"); print_r($stuff); ?> Array ( [name] => Chuck [course] => SI664 ) Trường Đại Học Bách Khoa TP.