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

» Easily "Force Quit" or "End Task" on a program in Linux with a keyboard shortcut

Note: This guide is written for an Ubuntu-based system but should work on any Linux distribution running the Gnome desktop environment.If a program stops responding in Linux, an easy way to kill it is with the xkill utility. This utility turns the mouse into crosshairs. Once the user clicks on a window with the crosshairs, said window is instantly killed. This is much more convenient than trying to find the process ID or name in a list of running processes and stopping it. In order to set up...

» PHP - Read in a file

The following function demonstrates how to read in a local file with PHP. You must first use the function to open the file handle. This tells PHP where to look to find this file. After that, it is simply a matter of iterating over the file until there are no more lines. The function tests if it has reached the end of the file yet - returning true if it has and false if there is still more to be read. Once we know there is another line to be read in, reads in the line for us into a slot of...

» How to Get the Latest Version of a Program in Ubuntu

Ubuntu, the popular Linux distribution, is well-known for its stability. This stability is due in part to its 6-month release cycle. Main programs included with Ubuntu are only updated every 6 months. This ensures that the developers have adequate time to test the software for regressions, bugs, and instabilities. However, sometimes it is nice to install the latest version of a program. This tutorial will use the application launcher Kupfer (like Gnome Do or Launchy) but the same procedure...