Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.

Similar presentations


Presentation on theme: "Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special."— Presentation transcript:

1 Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special markup that indicates you are about to start using PHP looks like this: <?php … At the end of your PHP, use the closing markup: ?>

2 Variables and ConstantstMyn2 In the classroom PCs the configuration has been implemented so that each PC does have a Web server software (Apache HTTP Server) of it’s own. All the PHP files must be saved to the directory C:\Program Files\Apache Group\Apache2\htdocs\YourPersonalFoulderName This means that http://localhost will display the home page of the local web site. Let’s test the first PHP page. Make sure the web server is running (it should always be running by default)

3 Variables and ConstantstMyn3

4 Variables and ConstantstMyn4

5 Variables and ConstantstMyn5 From the previous example we saw that you can intersperse your PHP code with HTML. So the HTML will be displayed by your browser, and the PHP will be run on your server – and if that PHP generates some HTML, that HTML will be displayed in your browser as well. You can insert PHP anywhere in your HTML page and the PHP engine on the web server will run it, as long as it is contained in the markup. When that PHP is run, any HTML it generates will be inserted into the page at the location of that PHP.

6 Variables and ConstantstMyn6 We used echo to display text using double quotes. Even if we could call echo as being a function, technically speaking echo is built-in PHP language construction. PHP statement ends with a semicolon. Never forget that when you work with PHP online, you are interacting with the user through a browser. That means that the text you send back to the browser will be interpreted as HTML, not just simple text. That also gives you the chance to make use of HTML to format your text:

7 Variables and ConstantstMyn7

8 Variables and ConstantstMyn8

9 Variables and ConstantstMyn9 You can also separate the items you want to print with commas:

10 Variables and ConstantstMyn10

11 Variables and ConstantstMyn11

12 Variables and ConstantstMyn12 You can also assemble text strings together into one string using a dot. In that case, PHP takes your expression and assembles it together (concatenation) into one single string:

13 Variables and ConstantstMyn13

14 Variables and ConstantstMyn14

15 Variables and ConstantstMyn15 There are three types of comments in PHP. The first type of comment lets you create multiline comments, which start with /* and ends with */. Single-line comment starts with // or # :

16 Variables and ConstantstMyn16

17 Variables and ConstantstMyn17

18 Variables and ConstantstMyn18 Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*‘. By default, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other.

19 Variables and ConstantstMyn19 PHP also offers another way to assign values to variables: assign by reference. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa. To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).

20 Variables and ConstantstMyn20

21 Variables and ConstantstMyn21

22 Variables and ConstantstMyn22 It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type depending on the context in which they are used - booleans default to FALSE, integers and floats default to zero, strings (e.g. used in echo()) are set as an empty string and arrays become to an empty array.

23 Variables and ConstantstMyn23 PHP provides a large number of predefined variables to all scripts. The variables represent everything from external variables to built-in environment variables, last error messages to last retrieved headers. Some examples: $_GET An associative array of variables passed to the current script via the URL parameters. $_POST An associative array of variables passed to the current script via the HTTP POST method. $_SESSION An associative array containing session variables available to the current script.

24 Variables and ConstantstMyn24 PHP does not require that you create different types of variables for different types of data:

25 Variables and ConstantstMyn25

26 Variables and ConstantstMyn26

27 Variables and ConstantstMyn27 There is a shortcut way of displaying the values of the variables in the previous example, called string interpolation. When you use interpolation, you only have to place the variable whose value you want to insert inside a double- quoted text string (not single-quoted!!):

28 Variables and ConstantstMyn28

29 Variables and ConstantstMyn29

30 Variables and ConstantstMyn30 Sometimes, you don’t want a data item to be a variable. For example, the value of pi shouldn’t change. The thing to do here is to create a constant, whose value can’t be modified. You create a constant in PHP with the define function:

31 Variables and ConstantstMyn31

32 Variables and ConstantstMyn32

33 Variables and ConstantstMyn33 The define() function defines a constant. Constants are much like variables, except for the following differences:  A constant's value cannot be changed after it is set  Constant names do not need a leading dollar sign ($)  Constants can be accessed regardless of scope  Constant values can only be strings and numbers Syntax define(name,value,case_insensitive) ParameterDescription nameRequired. Specifies the name of the constant valueRequired. Specifies the value of the constant case_insensitiveOptional. Specifies whether the constant name should be case-insensitive. If set to TRUE, the constant will be case-insensitive. Default is FALSE (case-sensitive)

34 Variables and ConstantstMyn34 PHP does you a big favor (?) by letting you store data without having to specify the data’s type. Sometimes, however, you have to know about the internal data types that PHP uses, such as: –boolean: Holds true/false values –integer: Holds numbers like -1, 0, 5 and so on –float: Holds floating-point numbers like 3.141592, 2.789321 –string: Holds text like “PHP should be fun!” –array: Holds arrays of data items

35 Variables and ConstantstMyn35 Let’s do some tests. In the beginning there is a variable which is set to “0”. So even if it seems to be an integer number zero, it is actually a string “0”. What happens, if we try to add integer value 1 to that variable’s value?:

36 Variables and ConstantstMyn36

37 Variables and ConstantstMyn37

38 Variables and ConstantstMyn38 In this case, PHP does the best it can: adding the integer value 1 to the string “0” leaves $stringVariable holding the integer 1. What if you add a floating point value to the string “0”?:

39 Variables and ConstantstMyn39

40 Variables and ConstantstMyn40

41 Variables and ConstantstMyn41 Let’s still try to confuse PHP:

42 Variables and ConstantstMyn42

43 Variables and ConstantstMyn43


Download ppt "Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special."

Similar presentations


Ads by Google