20 Apr 2013

What is Variable in php ?

Variables are used to make processes we script out more dynamic. In the programming world the word variable means
"a symbolic name associated with a value and whose associated value may be changed". 

PHP sports a cool feature called automatic data typing. A PHP developer can claim variables and use them in most common situations without having to claim the data type that the variable is.

How to create variable
  1. PHP variables must made by placing a dollar sign [ $ ]  and then the name of the variable directly after it.
  2. Variable name must starts from character or underscore.
  3. Do not use a NUMBER to start a variable name.
example













Predefined Variables
There are a set of predefined variables in PHP that are always available for use in any script or scope in your applications. When data is created, sent, or stored using mechanisms that create superglobal variables, we can then access those variables site-wide or all throughout our scripts and scope. Predefined superglobals are available to all of your scripts that are part of a website or application you are creating.

The superglobal variables are:
$_GET Stores any variables created using the GET method
$_POST Stores any variables created using the POST method
$_REQUEST Stores any variables created through a user input script (it can access both POST or GET)
$_FILES Stores any file upload variables created through user input scripts
$_SESSION Stores any variables created through registering session variables
$_COOKIE Stores any variables created through setcookie
$GLOBALS Stores any variables that have been globally defined
$_SERVER Stores server information such as headers, file names, reference paths, and current page.
$_ENV Stores any variables associated with the server environment.

example
















Magic Constants (Predefined)


What is echo and print

print and echo are both used to output data to browser software or other technologies that intake external data. As you come to view different PHP scripts in your travels online you may notice some authors use echo and some use print. Let us discuss the difference.

echo() = is a language construct, so you are not required to use parentheses with it. If you ever need to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

print() = behaves as a Function, but is not actually a function. print is sometimes mistaken as a function by many programmers since it sets a return value, but it is also a language construct like echo. It simply outputs a string of data. And you are not required to use parentheses with it.

Example code













Output will be the same.

how to use Code Comments PHP Scripts


Commented lines do not get processed by the browser or PHP. They get skipped over like they do not exist. They are made only for human reading.

Why programmer use code comment ?

1. Remind us what our code sections are doing long after we have created them.
2. In good HTML editors it makes a nice colored divider between code sections.
3. Place author name and script creation date information.
4. Make it easier for other developers and programmers to adapt and work with your code.

 In below example there are different types of comment code .