From 57a063416c81b0905329303597e52afd2ec92cfd Mon Sep 17 00:00:00 2001 From: chiffonsigh Date: Mon, 22 Oct 2018 20:47:36 -0400 Subject: [PATCH] PHP variable & data type edits (#19651) --- guide/english/php/data-types/index.md | 13 ----- guide/english/php/php-variables/index.md | 63 ---------------------- guide/english/php/variable-basics/index.md | 22 -------- guide/english/php/variables/index.md | 39 ++++++++++++-- 4 files changed, 34 insertions(+), 103 deletions(-) delete mode 100644 guide/english/php/data-types/index.md delete mode 100644 guide/english/php/php-variables/index.md delete mode 100644 guide/english/php/variable-basics/index.md diff --git a/guide/english/php/data-types/index.md b/guide/english/php/data-types/index.md deleted file mode 100644 index 0a30641f2e..0000000000 --- a/guide/english/php/data-types/index.md +++ /dev/null @@ -1,13 +0,0 @@ ---- -title: Data Types ---- -## Data Types - -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - - - -#### More Information: - diff --git a/guide/english/php/php-variables/index.md b/guide/english/php/php-variables/index.md deleted file mode 100644 index 0ab6300a1d..0000000000 --- a/guide/english/php/php-variables/index.md +++ /dev/null @@ -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 - -``` - -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) diff --git a/guide/english/php/variable-basics/index.md b/guide/english/php/variable-basics/index.md deleted file mode 100644 index f857e5d461..0000000000 --- a/guide/english/php/variable-basics/index.md +++ /dev/null @@ -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 - >> 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. diff --git a/guide/english/php/variables/index.md b/guide/english/php/variables/index.md index fa8022a093..57262e2146 100644 --- a/guide/english/php/variables/index.md +++ b/guide/english/php/variables/index.md @@ -5,21 +5,25 @@ title: Variables ## 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:** ```php ``` -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: When you assign a text value to a variable, put quotes around the value. +##### 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: 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 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 _ ) +* 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) +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 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. 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: + +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)