PHP Variables
Variables are named "containers" that allow you to store a value. This value can then be used by other parts of the application, simply by referring to the variable's name. For example, the application could display the contents of the variable to the user.In PHP, variable names must start with a dollar sign (
$). For example:| Code |
|---|
|
|
Another Example
PHP variables can contain strings (like the previous example), numbers, and arrays. When a variable contains a number, you can perform calculations against that value. Here's a simple calculation using variables:| Code |
|---|
|
|
11
Concatenation
When outputting multiple variables and other text to the screen, you can concatenate them (join them together) either with multiple echo statements, or by using one echo statement with a dot (.) between each part.Here's an example of both methods:
Option 1
| Code |
|---|
|
|
Option 2 (less code)
| Code |
|---|
|
|
2 + 9 = 11
Variable Names
When creating names for your variables, you need to ensure they comply with the following naming rules:- All PHP variable names must start with a letter or underscore (
_)" - Variable names can only contain alpha-numeric characters and underscores ( i.e.
a-z,A-Z,0-9, or_) - Variable names must not contain spaces. For multi-word variable names, either separate the words with an underscore (
_) or use capitalization.
PHP Form Variables
Many website applications rely on HTML forms so that users can perform their tasks. For example, most Content Management Systems allow users to provide content for a website by entering the content into a textarea form field, then clicking a "Save" button. When the user clicks the Save button, the form is submitted to the action page. The action page is normally specified with the action attribute of the form tag.If you're not familar with HTML forms, see the HTML forms section of the HTML tutorial, then return to this page.
Once a form has been submitted, the form fields are made available to the action page as a special type of variable. You, as the programmer, can read these form variables by using the appropriate syntax for form variables. Form variables are stored as an array. We will be covering arrays later, but for now all you need to know is how to read each form variable.
Forms can be submitted using one of two methods: get or post. By default, a form will be submitted using the "get" method. This results in each form variable being passed via the URL. If you decide to use the "post" method, you specify this using the method attribute (
method="post" ) of the form tag.The Get Method
If you use the get method, your action page can access each form field using$_GET["variableName"] (where "variableName" is the name of the form field).Example
Form page| Code | Result |
|---|---|
|
|
Here, the action page outputs the contents of the form variables that were passed from the form.
| Code |
|---|
|
|
The Post Method
If your form uses the post method, you use$_POST["variableName"].Example
Form page| Code | Result |
|---|---|
|
|
Here, the action page outputs the contents of the form variables that were passed from the form.
| Code |
|---|
|
|
PHP If Statements
You use PHP if statements when you want your program to execute a block of code only if a particular condition is true. In other words, you can tell your program "if something is true, then execute this piece of code".Syntax
| Code |
|---|
|
|
Example
In the following example, we create a variable called "favoriteFruit" and assign a value to it. We then compare the value with a string: "Pomegranate". If the two values are the same, we output some text.The two equals signs (
==) is a comparison operator (it compares the two values). If this condition is true, it displays the code within the curly braces ({ and }).
The curly braces are only necessary if you're outputting multiple lines
of code . If you're only outputting one line of code (like we are)
they're optional.| Code |
|---|
|
|
Your favorite fruit contains around 7% fibre.
If Else Statement
We can add an else to our if statement to make our application do something else if the condition is not true.Example:
| Code |
|---|
|
|
Sorry, I don't know how much fibre that fruit contains.
If... Elseif Statement
Let's say we learn the fibre content of another fruit. We could then add an elseif to our if statement. That way, we could include a custom message for the new fruit. In fact, we could use elseif as many times as we like.Example:
| Code |
|---|
|
|
Your favorite fruit contains around 1.5% fibre.
No comments:
Post a Comment