PHP variable & data type edits (#19651)

This commit is contained in:
chiffonsigh
2018-10-22 20:47:36 -04:00
committed by Kristofer Koishigawa
parent cfe2b24ab3
commit 57a063416c
4 changed files with 34 additions and 103 deletions

View File

@ -1,13 +0,0 @@
---
title: Data Types
---
## Data Types
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/agile/alignment/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
#### More Information:
<!-- Please add any articles you think might be helpful to read before writing the article -->

View File

@ -1,63 +0,0 @@
---
title: PHP Variables
---
## Variables
Variables are "containers" for storing information. Variables are declared using the dollar ($) sign followed immediately by the variable name. For example, the code block below would create a variable called `myVariable` and assign the string `Hello World` to it.
```php
<?php
$myVariable = "Hello World";
$x = 5;
$y = 10.5;
$z = '42';
?>
```
After the execution of the statements above, the variable `$myVariable` will hold a string with a value of Hello world!, the variable `$x` will hold a integer with a value of 5, and the variable `$y` will hold a float with a value of 10.5, and the variable `$z` will hold a string with a value of 42.
# Naming Variables
As with any programming language, PHP has certain rules that apply to naming variables. Valid variable names will follow the following rules
* A variable must start with the $ sign, followed by the name of the variable
* A variable name must start with a letter or the underscore character
* A variable name cannot start with a number
* A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
* Variable names are case-sensitive ($age and $AGE are two different variables)
# Predefined Variables
PHP has several special keywords that while are "valid" variable names, cannot be used for your variables. The reason for this is that the language itself has already defined those variables and they have are used for special purposes. Several examples are listed below, for a complete list see the [PHP documentation site](https://secure.php.net/manual/en/language.variables.predefined.php).
- `$this`
- `$_GET`
- `$_POST`
- `$_SERVER`
- `$_FILES`
# Assigning Values to Variables
To assign a value to a variable, you simply type the variable followed by the equals operator ( = ) followed by the value. For example
``` PHP
$myVariable = "Hello World";
$number1 = 5;
$number2 = 10;
$total = $number1 + $number2;
```
You may have noticed several important things about the example above. The first variable I declared it equal to **Hello World**, surrounded by quotation marks. This is because **Hello World** is a string of text and strings must be surrounded by quotation marks.
The second line I declared `$number1` to be equal to the value of 5. I could have declared `$number1` to be equal to `"5"`, which would tell PHP I want the 5 to be stored as a string, not a actual value. The difference is that you cannot perform calculation (as I did in the 4th line) on strings.
The fourth line I declare `$total` to be equal to the values of `$number1` plus `$number2`. This is called declaring a value by reference.
# PHP is a Loosely Typed Language
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.
# Conclusion
PHP makes it easy to work with variables, and you should think of variables as containers to store information. For even more information check out these resources:
- [PHP Variable Documentation](http://php.net/manual/en/language.variables.php)
- [W3Schools PHP Variables](https://www.w3schools.com/php/php_variables.asp)
- [PHP Data Types](https://guide.freecodecamp.org/php/data-types)

View File

@ -1,22 +0,0 @@
---
title: PHP Variable Basics
---
Variables allow programmers to use data throuhgout a PHP script.
In PHP, variables always start with a `$` symbol followed by the name of the variable. Only letters, numbers (may not be the first character), and underscores can make up the name of a variable.
For example, `$my_variable`, `$anotherVariable`, and `$the2ndVariable` are allow valid variable names.
Variable names are case-sensitive. `$my_variable` is different from `$My_Variable` which is different from `$mY_vARiAblE`.
Before a variable can be used, it must have a value assigned to it.
````PHP
<?php
$my_number = 1;
echo($my_number);
>>> 1
````
In the example above, the variable is `$my_number`. The value assigned to it was the number `1`. The variable was then passed as a parameter to the `echo` function, which output the value to the command line.

View File

@ -5,21 +5,25 @@ title: Variables
## Variables ## Variables
# Creating (Declaring) PHP Variables # Creating (Declaring) PHP Variables
Variables are "containers" for storing information. Variables are "containers" for storing information. They are the main way to store information in a PHP program.
All variables in PHP are denoted with a leading dollar sign like "$variable_name".
Variables are assigned with the "=" operator, with the variable on the left-hand side and the expression to be evaluated on the right.
**Syntax:** **Syntax:**
```php ```php
<?php <?php
// Assign the value "Hello world" to the variable "txt"
$txt = "Hello world!"; $txt = "Hello world!";
// Assign the value "5" to the variable "x"
$x = 5; $x = 5;
// Assign the value "10.5" to the variable "y"
$y = 10.5; $y = 10.5;
?> ?>
``` ```
After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5. ##### Note: Using quotes or not using quotes will change the type of variable created. [Read more](https://guide.freecodecamp.org/php/data-types) about variable and data types.
##### Note: When you assign a text value to a variable, put quotes around the value.
##### Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it. ##### Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.
@ -28,9 +32,24 @@ After the execution of the statements above, the variable $txt will hold the val
* A variable starts with the $ sign, followed by the name of the variable * A variable starts with the $ sign, followed by the name of the variable
* A variable name must start with a letter or the underscore character * A variable name must start with a letter or the underscore character
* A variable name cannot start with a number * A variable name cannot start with a number
* A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) * A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) You cannot use characters like `+ , - , % , ( , ) . &` in its name.
* Variable names are case-sensitive ($age and $AGE are two different variables) * Variable names are case-sensitive ($age and $AGE are two different variables)
Some examples of allowed variable names:
* $my_variable
* $anotherVariable
* $the2ndVariable
# Predefined Variables
PHP has several special keywords that while are "valid" variable names, cannot be used for your variables. The reason for this is that the language itself has already defined those variables and they have are used for special purposes. Several examples are listed below, for a complete list see the [PHP documentation site](https://secure.php.net/manual/en/language.variables.predefined.php).
- `$this`
- `$_GET`
- `$_POST`
- `$_SERVER`
- `$_FILES`
# Output Variables # Output Variables
The PHP echo statement is often used to output data to the screen. The PHP echo statement is often used to output data to the screen.
@ -66,6 +85,16 @@ In the example above, notice that we did not have to tell PHP which data type th
PHP automatically converts the variable to the correct data type, depending on its value. PHP automatically converts the variable to the correct data type, depending on its value.
In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it. In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.
# Variable lifecycle
In PHP variables have a default value. If a variable is not declared before you attempt to use it, its value will be NULL. It is unset. So you can't use it by writing "isset($variable)" before using it.
#### More Information: #### More Information:
For even more information check out these resources:
- [PHP Variable Documentation](http://php.net/manual/en/language.variables.php)
- [W3Schools PHP Variables](https://www.w3schools.com/php/php_variables.asp)
- [PHP Data Types](https://guide.freecodecamp.org/php/data-types)
Learn about the different types of variables you can create: [Data Types](https://guide.freecodecamp.org/php/variables/data-types)
<!-- Please add any articles you think might be helpful to read before writing the article --> <!-- Please add any articles you think might be helpful to read before writing the article -->