1 / 43

Variables and Constants

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

Download Presentation

Variables and Constants

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: ?> 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) tMyn

  3. tMyn

  4. 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. 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: tMyn

  7. tMyn

  8. tMyn

  9. You can also separate the items you want to print with commas: tMyn

  10. tMyn

  11. 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: tMyn

  13. tMyn

  14. 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 #: tMyn

  16. tMyn

  17. 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. 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). tMyn

  20. tMyn

  21. 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. 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. • $_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. tMyn

  24. PHP does not require that you create different types of variables for different types of data: tMyn

  25. tMyn

  26. 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!!): tMyn

  28. tMyn

  29. 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: tMyn

  31. tMyn

  32. 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) 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 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?: tMyn

  36. tMyn

  37. 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”?: tMyn

  39. tMyn

  40. tMyn

  41. Let’s still try to confuse PHP: tMyn

  42. tMyn

  43. tMyn

More Related