Chapter 3 Introduction to PHP
description
Transcript of Chapter 3 Introduction to PHP
![Page 1: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/1.jpg)
Chapter 3 Introduction to PHP
![Page 2: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/2.jpg)
Incorporating PHP Within HTML
• By default, PHP documents end with the extension .php• files ending with .htm or .html to also get parsed by the
PHP processor
• To trigger the PHP commands, you need to learn a new tag. The first part is: <?php the closing part is encountered, which looks like this: ?>
![Page 3: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/3.jpg)
Calling the PHP Parser
• A small PHP “Hello World” program might look like<?phpecho "Hello world";?>
• all the examples from this book have been archived onto a specially created companion website at http://lpmj.net
![Page 4: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/4.jpg)
Using Comments
• There are two ways in which you can add comments• // This is a comment• after a line of code $x += 10; // Increment $x by 10• When you need multiple-line comments
<?php/* This is a sectionof multiline commentswhich will not beinterpreted */?>
![Page 5: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/5.jpg)
Basic Syntax
• Semicolons$x += 10;
You must place a $ in front of all variables<?php$mycounter = 1;$mystring = "Hello";$myarray = array("One", "Two", "Three");?>
![Page 6: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/6.jpg)
Understanding Variables
• String variables $username = "Fred Smith";
<?php // test1.php$username = "Fred Smith";echo $username;echo "<br />";$current_user = $username;echo $current_user;?>
![Page 7: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/7.jpg)
Numeric variables
• $count = 17; to store the number 17 in the variable $count• $count = 17.5; a floating-point number (containing a decimal
point);• Arrays $team = array('Bill', 'Joe', 'Mike', 'Chris', 'Jim');• Two-dimensional arrays
<?php$oxo = array(array('x', '', 'o'),array('o', 'o', 'x'),array('x', 'o', '' ));?>
![Page 8: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/8.jpg)
Variable naming rules
• four 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 it should be separated with the _ (underscore) character. (e.g., $user_name).
• • Variable names are case-sensitive. The variable $High_Score is not the same as the variable $high_score.
![Page 9: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/9.jpg)
Operators
• Operators are the mathematical, string, comparison, and logical commands such as plus, minus, times, and divide. PHP looks a lot like plain arithmetic; for instance, the following statement outputs 8:
echo 6 + 2;
![Page 10: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/10.jpg)
Arithmetic operators
![Page 11: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/11.jpg)
Assignment operators
![Page 12: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/12.jpg)
Comparison operators
![Page 13: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/13.jpg)
Logical operators
![Page 14: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/14.jpg)
Variable Assignment
• The syntax to assign a value to a variable is always variable = value. Or, to reassign the value to another variable, it is other variable = variable.
• Variable incrementing and decrementing• String concatenation uses the period (.) to append one
string of characters to another. The simplest way to do this is as follows:
echo "You have " . $msgs . " messages.";
![Page 15: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/15.jpg)
Multiple-Line Commands
• Example 3-6. A multiline string echo statement<?php$author = "Alfred E Newman";echo "This is a HeadlineThis is the first line.This is the second.Written by $author.";?>
![Page 16: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/16.jpg)
Variable Typing
• Variables do not have to be declared before they are used, and that PHP always converts variables to the type required by their context when they are accessed.
![Page 17: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/17.jpg)
Constants
• Constants are similar to variables, holding information to be accessed later, except that they are what they sound like—constant
![Page 18: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/18.jpg)
Echo and print Commands
• echo cannot be used as part of a more complex expression, whereas print can
$b ? print "TRUE" : print "FALSE";• The question mark is simply a way of interrogating
whether variable $b is true or false. Whichever command is on the left of the following colon is executed if $b is true, whereas the command to the right is executed if $b is false
![Page 19: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/19.jpg)
Functions
• A simple function declaration<?php
function longdate($timestamp){return date("l F jS Y", $timestamp);}?>
![Page 20: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/20.jpg)
Global variables
• To declare a variable as having global scope, use the keyword global
![Page 21: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/21.jpg)
A function using a static variable
<?phpfunction test(){static $count = 0;echo $count;$count++;}?>
![Page 22: Chapter 3 Introduction to PHP](https://reader035.fdocuments.in/reader035/viewer/2022062222/568164ae550346895dd6baba/html5/thumbnails/22.jpg)
PHP’s superglobal variables