Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext...

43
Variables and Cons tants tMyn 1 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: ?>

Transcript of Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext...

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

Variables and Constants

tMyn 1

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:

?>

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

Variables and Constants

tMyn 2

• 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)

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

Variables and Constants

tMyn 3

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

Variables and Constants

tMyn 4

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

Variables and Constants

tMyn 5

• 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 <?php … ?> markup. When that PHP is run, any HTML it generates will be inserted into the page at the location of that PHP.

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

Variables and Constants

tMyn 6

• 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:

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

Variables and Constants

tMyn 7

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

Variables and Constants

tMyn 8

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

Variables and Constants

tMyn 9

• You can also separate the items you want to print with commas:

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

Variables and Constants

tMyn 10

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

Variables and Constants

tMyn 11

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

Variables and Constants

tMyn 12

• 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:

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

Variables and Constants

tMyn 13

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

Variables and Constants

tMyn 14

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

Variables and Constants

tMyn 15

• 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 #:

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

Variables and Constants

tMyn 16

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

Variables and Constants

tMyn 17

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

Variables and Constants

tMyn 18

• 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.

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

Variables and Constants

tMyn 19

• 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).

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

Variables and Constants

tMyn 20

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

Variables and Constants

tMyn 21

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

Variables and Constants

tMyn 22

• 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.

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

Variables and Constants

tMyn 23

• 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.

• $_POSTAn associative array of variables passed to the current script via the HTTP POST method.

• $_SESSIONAn associative array containing session variables available to the current script.

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

Variables and Constants

tMyn 24

• PHP does not require that you create different types of variables for different types of data:

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

Variables and Constants

tMyn 25

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

Variables and Constants

tMyn 26

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

Variables and Constants

tMyn 27

• 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!!):

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

Variables and Constants

tMyn 28

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

Variables and Constants

tMyn 29

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

Variables and Constants

tMyn 30

• 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:

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

Variables and Constants

tMyn 31

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

Variables and Constants

tMyn 32

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

Variables and Constants

tMyn 33

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)

Parameter Description

name Required. Specifies the name of the constant

value Required. Specifies the value of the constant

case_insensitive Optional. Specifies whether the

constant name should be case-insensitive. If set to TRUE,

the constant will be case-insensitive. Default is FALSE

(case-sensitive)

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

Variables and Constants

tMyn 34

• 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

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

Variables and Constants

tMyn 35

• 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?:

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

Variables and Constants

tMyn 36

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

Variables and Constants

tMyn 37

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

Variables and Constants

tMyn 38

• 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”?:

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

Variables and Constants

tMyn 39

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

Variables and Constants

tMyn 40

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

Variables and Constants

tMyn 41

• Let’s still try to confuse PHP:

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

Variables and Constants

tMyn 42

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

Variables and Constants

tMyn 43