Variables in PHP

(0 votes, average 0 out of 5)

A variable is a way to store information in may program and scripting languages. In PHP, variables can store things such as strings, integers, floats, boolean, or other objects.

To definite and initialize a variable in PHP, you simply use the following form within your code: 

1
$variable_name = value;

When creating a new variable in PHP, you must follow the simple naming conventions below:

  • Variables must start with a letter or underscore ("_").
  • Variables with more than one word should be separated with underscores (i.e. $new_variable) or distinguished with capitalization (i.e. $newVariable).
  • Variables may only named with alpha-numeric characters and underscores (i.e. $new_variable01).
  • Variables should NOT contain spaces.

A few notes about PHP variables:

  • You must include the dollar sign at the beginning of the variable in order for the PHP to parse it correctly.
  • Variables do not be to be declared before they are initialized.

You must include the dollar sign at the beginning of the variable in order for PHP to parse it correctly. 

Partner Links:
 
Related Articles

» Using Cookies to Save Data in PHP

Cookies are a useful way to save information, for example a user preference or login session locally on the client's machine. PHP makes creating and accessing cookies very easy. To create a cookie called myCookie with the value hello,world, simply add the following line of PHP code:$expireTime = 60 * 60 * 24 + (); // 1 day("myCookie", "hello,world", $expireTime); This data will then be available the next time the user loads that particular page. To access the cookie at a later date, use the...

» Java - Reading and writing a properties file

A properties file in Java can be very useful when you start to notice that there are many locations in your code where you are either hard coding values, many constants, or using enums. Instead of including these values directly in your code, you can refactor them out to a properties file to separate responsibilities. Instead of potentially specifying values throughout many classes or programs, all values can be condensed into one or more properties files as need.An example of a properties file...

» Conditional (If-Else) Statements in Python

Below is a quick example of how to use an if statement in python: 12345if x==3: print "We are inside an if statement" x = x + 1print "we are no longer in an if statement" Note that unlike most languages, no parentheses are needed, although including parentheses does not violate python syntax.  Anything indented after the if statement is included in the execution if the if statement evaluates to true.  The scope is closed once indentation returns to the indent location of the...