Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 2: PHP Language Basics iNET Academy Open Source Web Development.

Similar presentations


Presentation on theme: "Session 2: PHP Language Basics iNET Academy Open Source Web Development."— Presentation transcript:

1 Session 2: PHP Language Basics iNET Academy Open Source Web Development

2 Objectives PHP & HTML text Discuss variables’s definition, access, types and scope Discuss string’s definition, comparison and concatenation Discuss constants and predefined constants Discuss mathematical operations

3 Embedding PHP into HTML code

4 Comments Single line comment: // Multiline comment: start with /* and end with */ All PHP statements end with a semi-colon (;) Start a new line after your semi-colon

5 Variables A variable stores a value in memory In PHP, define a variable using the following form $variable_name = value; Rules of writing variable’s form The dollar sign ($) must fill the first place The first character after the $ must be either a letter (a-z|A-Z) or an underscore (_) Variable’s name is composed only of alphanumeric characters and underscore Variable’s in PHP are case-sensitive Variables with more than one word can be separated with underscores to make them easier to read Variables can be assigned values using the sign (=)

6 Variables (cont.) Assign value Reassign values Reading a variable’s value: specify the ($) followed by the variable’s name Variables are temporary. PHPautomatically cleans them when programs finish. Variable’s types: strings, numbers and arrays PHP handles data types automatically based on the context of using. Variable scope Variable’s value and type can be overwritten in a program Variable scope created by using function

7 Variables (cont.) Example of a variable scope To access a variable outside of a function, use global variable

8 Variables (cont.) Global variables Be carefull when using global variable If you want to use a variable in specific function without losing the value when the function ends, you would use static variable

9 Variables (cont.) Static variables are not destroyed when a function ends

10 Variables (cont.) Super global variables: provide information about PHP script’s environment defined in arrays since PHP 4.01

11 Strings Strings are sequences of individual characters String can be used directly in a function call or stored in a variable Strings are flexible Embed a variable in a string Single quotes are used if there are no embedded variables

12 Strings (cont.) Special characters: tab, new line, carriage return … To print special characters, string must use double quote and escaped characters Escape quotes within the string with a slash Use single quotes for quotes inside the string Start and end the string with single quotes

13 Strings (cont.) When single quote is used to define the string, double quotes don’t need to be escaped.

14 Strings (cont.) Comparing strings: use strcmp(string1, string2) function to compare two strings including the case Return value is 0 if two strings have the same text Nonzero value is returned if two strings are not the same use strcasecmp(string1, string2) function to compare two strings without comparing the case Return value is 0 if two strings have the same text Nonzero value is returned if two strings are not the same

15 Strings (cont.)

16 Concatenation: combines one or more text strings and variables. Use a period (.) to join variables and text strings. Can be done multiple times Combining a string with another data type results a new string

17 Constants A constant can not change its value during the execution of the program Constants are global and can be any simple data type Access a constant’s value using its name or using the constant function

18 Constants (cont.) Differences between constants and variables It’s common practice to capitalize a variable name for constants Constants do not have a dollar sign ($) at the start of their names Constants can be defined only by using the define function, not by simple assignment Constants are defined and accessed globally Constants cannot be redefined or undefined once they have been set Constants can evaluate only to scalar values

19 Constants (cont.) Predefined constants: similar to super global variables __FILE__ : returns the name of the PHP file being executed __LINE__ : returns the line number in that file __FUNCTION__ : the function name __CLASS__ : the class name __METHOD__ : the class method name

20 PHP basic mathematical operators

21 Combined Assignment Performing two common tasks at the same time Reading a variable’s value Performing operation on it Placing the result back in the same variable

22 Auto increment

23 Preincrement and -decrement

24 Practice In this practice, you will Download the session2_ex1.html from \\dc Replace the html corresponding to each row of the table with an echo statement Store the value of each cell in a variable Store the header’s content of the table in a constant


Download ppt "Session 2: PHP Language Basics iNET Academy Open Source Web Development."

Similar presentations


Ads by Google