SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Variables

PHP Example #14

Variables

Variables admit text, numbers or the values of other variables. The names of variables are case-sensitive and must begin with a $ symbol followed by a letter or an underscore, while the rest of the characters can be letters, numbers or underscores. Double quoted strings allow to interpolate variables, this is, the name of the variable will be replaced by the stored value and not printed like it were text. When interpolating variables within strings, if the PHP interpreter can be confused with the name of the variable, we can use brackets, enclosing the name, to avoid the confusion.

<?php
$price = 3.95; print 'Price: '; print $price; print nl2br("\n");
$tax_rate = 0.08; print 'Tax rate: '; print $tax_rate; print nl2br("\n");
$tax_amount = $price * $tax_rate; print 'Tax amount: '; print $tax_amount; print nl2br("\n");
$total_cost = $price + $tax_amount; print 'Total cost: '; print $total_cost; print nl2br("\n");
$username = 'james'; print 'Username: '; print $username; print nl2br("\n");
$domain = '@example.com'; print 'Domain: '; print $domain; print nl2br("\n");
$email_address = $username . $domain; print 'E-mail address: '; print $email_address; print nl2br("\n\n");
print 'The tax is ' . $tax_amount; print nl2br("\n");
print 'The total cost is ' . $total_cost; print nl2br("\n");
print 'E-mail: ' . $email_address; print nl2br("\n\n");
$email = 'jacob@example.com';
print "Send replies to: $email"; print nl2br("\n\n");
$price = $price + 3; // Add 3 in the habitual form
$price += 3; // Add 3 with the special operator
$username = $username . $domain; // Join $domain at the end of $username in the habitual form
$username .= $domain; // Join $domain at the end of $username with the special operator
$birthday = 10;
$birthday = $birthday + 1; // Add 1 to $birthday in the habitual form
++ $birthday; // Add 1 to $birthday with the special operator
$years_left = 10;
$years_left = $years_left - 1; // Rest 1 to $years_left in the habitual form
-- $years_left; // Rest 1 to $years_left with the special operator
$preparation = 'Braise';
$meat = 'Beef ';
$vegetables = 'Bean sprout';
print "{$preparation}d $meat with $vegetables";
?>
Price: 3.95
Tax rate: 0.08
Tax amount: 0.316
Total cost: 4.266
Username: james
Domain: @example.com
E-mail address: james@example.com

The tax is 0.316
The total cost is 4.266
E-mail: james@example.com

Send replies to: jacob@example.com

Braised Beef  with Bean sprout

PHP Example #15

Variables storing HTML code

Variables are very powerful. They admit not only text, numbers or the values of other variables, but as well pieces of HTML code. This allows us to place an HTML code in different locations inside a webpage without having to repeat the code itself. We can as well use variables within the piece of HTML code and assign values to them, but we must then enclose the code within double quotes.

<?php
$list = '<ul>
<li>Beef Chow-fun</li>
<li>Sauteed Pea Shoots</li>
<li>Soy Sauce Noodles</li>
</ul>';
print $list;
print nl2br("\n");
print $list;
print nl2br("\n");
print $list;
print nl2br("\n");
$dish_1 = 'Barbecued meat';
$dish_2 = 'Salad';
$dessert = 'Custard';
$var_list = "<ul>
<li>First dish: $dish_1</li>
<li>Second dish: $dish_2</li>
<li>Dessert: $dessert</li>
</ul>";
print $var_list;
?>
  • Beef Chow-fun
  • Sauteed Pea Shoots
  • Soy Sauce Noodles

  • Beef Chow-fun
  • Sauteed Pea Shoots
  • Soy Sauce Noodles

  • Beef Chow-fun
  • Sauteed Pea Shoots
  • Soy Sauce Noodles

  • First dish: Barbecued meat
  • Second dish: Salad
  • Dessert: Custard