Saving Data for Later PHP

Often it is necessary to save information for later use. PHP, like most programming languages, offers the concept of variables. Variables give a name to the information you want to save and manipulate. It expands on our example by using variables.

Assigning Values to Variables

Assigning Values to Variables

Assigning Values to Variables

The first block of PHP code puts values into some variables. The four variables are YourName, Today, CostOfLunch, and DaysBuyingLunch. PHP knows they are variables because they are preceded by a dollar sign ($). The first time you use a variable in a PHP script, some memory is set aside to store the information you wish to save. You don't need to tell PHP what kind of information you expect to be saved in the variable; PHP can figure this out on its own.

The script first puts a character string into the variable YourName. As I noted earlier, PHP knows it's textual data because I put quotes around it. Likewise I put today's date into a variable named Today. In this case PHP knows to put text into the variable because the date function returns text. This type of data is referred to as a string, which is shorthand for character string. A character is a single letter, number, or any other mark you make by typing a single key on your keyboard.

Notice that there is an equal sign (=) separating the variable and the value you put into it. This is the assignment operator. Everything to its right is put into a variable named to its left.

The third and fourth assignments are putting numerical data into variables. The value 3.5 is a floating-point, or fractional, number. PHP calls this type a double, showing some of its C heritage. The value 4 in the next assignment is an integer, or whole number.

After printing some HTML code, another PHP code block is opened. First the script prints today's date as a level-three header. Notice that the script passes some new types of information to the print function. You can give string literals or string variables to print and they will be sent to the browser.

When it comes to variables, PHP is not so lenient with case. Today and today are two different variables. Since PHP doesn't require you to declare variables before you use them, you can accidentally type today when you mean Today and no error will be generated. If variables are unexpectedly empty, check your case.
The script next prints Leon, you will be out 14.00 dollars this week. The line hat prints the total has to calculate it with multiplication using the * operator.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics