If you have never had any programming, Algebra, or
scripting experience, then the concept of variables
might be a new concept to you. A detailed explanation of variables is beyond the
scope of this tutorial, but we've included a refresher crash course to guide you.
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.
A variable can have a short name, like x, or a more descriptive name, like carName.
Rules for PHP variable names:
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
After the execution of the statement above, the variable myCar will hold the value
Volvo.
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create a variable containing a string, and a variable containing a number:
Note: When you assign a text value to a variable, put quotes around the value.
Note for programmers: PHP does not require variables to be declared before
being initialized.
PHP has four different variable scopes:
To do this, use the static keyword when you first declare the variable:
Then, each time the function is called, that variable will still have the
information it contained from the last time the function was called.
Note: The variable is still local to the function.
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again. In PHP you define a variable with the following form:
- $variable_name = Value;
Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.
PHP Variables
As with algebra, PHP variables are used to hold values or expressions.A variable can have a short name, like x, or a more descriptive name, like carName.
Rules for PHP variable names:
- Variables in PHP starts with a $ sign, followed by the name of the variable
- The variable name must begin with a letter or the underscore character
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- A variable name should not contain spaces
- Variable names are case sensitive (y and Y are two different variables)
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
$myCar="Volvo";
Tip: If you want to create a variable without assigning it a value, then you assign it the value of null.
Let's create a variable containing a string, and a variable containing a number:
<?php
$txt="Hello World!";
$x=16;
?>
$txt="Hello World!";
$x=16;
?>
A Quick Variable Example
Say that we wanted to store the values that we talked about in the above paragraph. How would we go about doing this? We would first want to make a variable name and then set that equal to the value we want. See our example below for the correct way to do this.PHP Code:
<?php $hello = "Hello World!"; $a_number = 4; $anotherNumber = 8; ?>
PHP Variable Scope
The scope of a variable is the portion of the script in which the variable can be referenced.PHP has four different variable scopes:
- local
- global
- static
- parameter
PHP Variable Naming Conventions
There are a few rules that you need to follow when choosing a name for your PHP variables.- PHP variables must start with a letter or underscore "_".
- PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
- Variables with more than one word should be separated with underscores. $my_variable
- Variables with more than one word can also be distinguished with capitalization. $myVariable
Static Scope
When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted.To do this, use the static keyword when you first declare the variable:
static $rememberMe;
Note: The variable is still local to the function.
0 comments:
Post a Comment