I hope you got some idea of how the php code will be strucured and how to show the content with php. If you still have doubts or not yet read then first please read it. You can find here php's structure and syntax.
Now let's move on to the next concept called "Variables". Can you imagine a language without variables? Just imagine. No way!! If a programming language doesn't provide data manipulation then that langauge will be in a great darkness. No one can use the language. So as programmer you have to choose different data types with respect to your application requirement.
What is a Variable?
A variable is a representation of a particular value, such as name of a person or price of something etc.
Its your sole responsibilty as a programmer that when dealing with data, its important for you to deal what kind of data you are dealing with. For example, for a Hospital management system if you ask a patient how many times you have been visited the hospital and in reply if he says 1.5 times then its purely illogical. Means from this statement we can say a patient can't visit 1.5 times. It may be 1 time or 2 times etc. Like wise if you ask the patient about his name and in reply if he says 12345 then its also illogical.
Like many other languages support different data types, PHP is not an exception it also supports different data types. Let's see.
How to create a Variable?
• To create a variable first of all look for a good name. Means if your application needs to store some name then name the variable as username. Means give some meaningful name which can be easy for you to remember.
• Put the dollar($) sign in front of the username. Like this $username.
• If you want to store some value or assign something to it then do like this. $username = "phphunger". You can use the assignment operator called = to assign some value to a variable.
That's it you have created the variable. Hurrah...
Rules for creating the variables
Rule 1: All variables must begin with a $ symbol and followed by a good name.
Eg: $username, $price, $gender
Rule 2: A variable can not start with a numeric character.
Eg: $1, $2username, $321price
Rule 3: A varible can contain numbers and underscore character(_).
Eg: $username123, $oneby2, $value_1
Rule 4: A variable can start with underscore character (_).
Eg: $_abc
Rule 5: A variable is case-sensitive.
Eg: $username and $USERNAME are two different variables.
What are the different types of variables?
PHP supports mainly 2 types of varibles. Under this there are so many types available. Let's see.
1. Scalar
2. Array
The scalar type variables holds only one value at a time and arrays holds list of values or another array.
Let's see different scalar variable types.
Numeric Values: Integer and Floating values
Integer Values: Whole numbers (numbers without decimals). Examples are 1, 345, and 9922786. You can also use octal and hexadecimal notation: The octal 0123 is decimal 83 and the hexadecimal 0x12 is decimal 18.
Floating point values: Numbers with decimals. Examples are 1.5, 87.3446, and 0.88889992.
String Values: Single quote, Double quote and Heredoc values
Text or numeric information, specified within double quotes (" ") or single quotes (' ').
The <<< heredoc tag is followed by an arbitrary string of text (which we’ll call the marker) on a single line. The interpreter will parse the contents of the file as a string until the marker is found, on its own, at the beginning of a line, followed by a semicolon. Heredoc strings can come in handy when you want to embed large amounts of text in your scripts—although you can sometimes achieve a similar goal by simply switching in and out of PHP mode.
Eg:
<<My text goes here.
More text can go on another line.
You can even use escape sequences: \t
ENDOFTEXT;
Boolean Values: TRUE or FALSE
Array values:
Arrays are an aggregate value—that is, they represent a collection of other values. In PHP, arrays can contain an arbitrary number of elements of heterogeneous type (including other arrays). Each element is assigned a key—another scalar value used to identify the element within the array. The arrays will be covered later in the future articles. Be patient.
Apart from these variables, there are some more types namely local and global variables, Predefined variables, Constant variables. Let's see these.
Local and Global variables: Local variables are available to that particular function in which they are defined where as gloabal variables are available to the entire script.
Predefined or (superglobals) variables: In all PHP scripts, a set of predefined variables are available to you. You might have seen some of these variables in the output of the phpinfo() function. Some of these predefined variables are called superglobals, meaning that they are always present and available to all of your scripts, without any intervention by you, the programmer.
• $_GET contains any variables provided to a script through the GET method.
• $_POST contains any variables provided to a script through the POST method.
• $_COOKIE contains any variables provided to a script through a cookie.
• $_FILES contains any variables provided to a script through file uploads.
• $_ENV contains any variables provided to a script as part of the server environment.
• $_SESSION contains any variables that are registered in a session.
Constant variables: A constant is an identifier for a value that cannot change during the course of a script. Once a constant has a value, it remains through its execution lifetime. Constants can be user defined, or you can use some of the predefined constants that PHP always has available. Unlike simple variables, constants do not have a dollar sign before their name, and they are usually uppercase to show their difference from a scalar variable.
Eg: define("MYCONSTANT", "Some Constant value");
Let's see a small demo on some of these varibales.
Did You Enjoy this Article ?
If yes, Then enter your email below to get
more such great articles in your inbox
For FREE !
cool explanation of variables...
ReplyDeletenice blog checkout my blogs at
ReplyDeletehttp://www.onjokes.blogspot.com
feel free to leave a comment