Php-mysql Course

  • Assign value by reference
  • Dynamic variables

Variables are used to store different kinds of data (or values). These values can be numbers, text, or much more complex data. PHP has eight basic (or primitive) types of variables.

PHP Data types

- Usually the variable type is not explicitly specified, PHP automatically converts the variable to the correct data type.

Using variables

Variables are used for storing values, data like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
Regardless of what type you are creating, all variables in PHP follow certain syntactical rules:
Variables can be assigned values using the equals sign (=).
<?php
$var_name = value;
?>

There are two ways to assign values to variables: by value and by reference.

Assign by value

The typical way to do assignments is to define by value. Value can be a number, a string (or any data type), or another variable previously defined.
<?php
$var1 = 8;                         // the value is a number
$var2 = 'coursesweb.net';      // the value is a string
$var3 = true;                      // a boolean data
$var4 = $var1;                     // the value of $var4 is 8 (the value of $var1)

echo $var4;                      // 8
?>
- The values of these variables remains intact until it is reassigned or the script has completed.

• If you assign another value to an existing variable, the new value will overwrite the old one.
<?php
$site = 'coursesweb.net';
$site = 789;                       // assigns another value, a number

echo $site;                       // 789
?>

• Variables can be printed within double quotation marks, but not within simple quotation marks. If you add a variable inside simple quotation marks, will display its name (not its value).
<?php
$name = 'Marius';
echo "Hy $name". '<br />';          // Hy Marius
echo 'Hy $name';                    // Hy $name
?>
• Dot character (.) is used to join strings.
Example:
<?php
$protocol = 'http://';
$site = $protocol . 'coursesweb.net';
echo $site;                  // https://coursesweb.net
?>

Assign value by reference

The reference approach allows the content of a variable to affect the value of another variable (previously defined), or a function to affect a variable that is not part of that function (see the lesson: Passing variable to function by reference).
To define a variable by reference, simply add the ampersand (&) character before the dollar sign ($).
<?php
$var1 = "Tutorials";                 // assigned by value
$var2 = &$var1;                      // $var1 is assigned to $var2 by reference

echo $var2;                // Tutorials

// changing the value of $var1 will modify also the $var2
$var1 = 'marplo.net';
echo '<br/>'. $var2;         // marplonet.net

// changing the value of $var2 will be transmitted to $var1 too
$var2 = 'https://coursesweb.net';
echo '<br/>'. $var1;                     // https://coursesweb.net ( because of the "by reference" )
?>
- Once a variable is assigned by reference, it is tied to its referenced variable. If one of them changes, will transfer the same change to the other one. If you change the value of $var1 will change the value of $var2 too.
Because assigning values by reference can complicate the script, it's better to avoid this.

Dynamic variables

A dynamic variable is named using two dollar signs ($$) and is associated with a normal variable that has a similar name.
The value of an usual variable gives the name (without the dollar symbol) to a second variable.
<?php
$good_people = 12;
$var_name = "good_people";
echo $$var_name;                   // 12

// assigning a value to $$var_name will change the value to $good_people
$$var_name = 'other value';
echo '<br />'. $good_people;              // other value
?>
A dynamic variable does not contain its own value. Instead, it contains the location where you can find the value, in other words, the name of another variable.
The expresions: $var_name = "good_people"; and $$var_name; make result a variable "$good_people", the value of "$$var_name" is the value of "$good_people".

- Dynamic variables, as those by reference can be confusing and it's better to avoid them.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Variables and Data Types

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142523)
  2. Pencil and Pen Tools (2262)
  3. Graphic Symbols (1512)
  4. CSS cursor property - Custom Cursors (6184)
  5. Responses (329)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (526)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)
Chat
Chat or leave a message for the other users
Full screenInchide