PHP/Language Basics/Variables
Содержание
- 1 A properly set-up PHP variable
- 2 Automatic Type Conversion
- 3 Calculate the variable name for simpler variables and use that name to access the value of that variable.
- 4 Numbers
- 5 Numeric Data Types
- 6 Operating on variables
- 7 PHP 5 variable functionality.
- 8 PHP Superglobal Arrays
- 9 PHP Variable Examples
- 10 Valid and invalid variable names
- 11 valid statements
A properly set-up PHP variable
<?php
$myvar = 0;
$yourvar = "This is my value<br />";
$myvar = $yourvar;
$myvar = "This is now my value.<br />";
echo $yourvar;
$myvar = &$yourvar;
$myvar = "This is now my value.<br />";
echo $yourvar;
?>
Automatic Type Conversion
<?
$mystring = "12";
$myinteger = 20;
print $mystring + $myinteger;
?>
Calculate the variable name for simpler variables and use that name to access the value of that variable.
<?php
$a0 = "This";
$a1 = "is";
$a2 = "a";
$a3 = "test";
for ($i = 0; $i < 4; $i++) {
$var = "a$i";
echo "${$var} ";
}
?>
Numbers
<?
print 6;
print 6.3;
print 6.30;
print 0.4422;
print 16.216;
print 0;
print -23;
print 1298317;
print -9912111;
print -12.52222;
print 0.00;
?>
Numeric Data Types
<?php
$a = 5;
$b = $a / 2;
echo "$a / 2 = $b\n";
$a = 6;
$b = $a / 2;
echo "$a / 2 = $b\n";
?>
Operating on variables
<?php
$price = 3.95;
$tax_rate = 0.08;
$tax_amount = $price * $tax_rate;
$total_cost = $price + $tax_amount;
$username = "james";
$domain = "@example.ru";
$email_address = $username . $domain;
print "The tax is " . $tax_amount;
print "\n"; // this prints a linebreak
print "The total cost is " .$total_cost;
print "\n"; // this prints a linebreak
print $email_address;
?>
PHP 5 variable functionality.
<?php
$myvar = 0;
$yourvar = "This is my value<br />";
$myvar = $yourvar;
$myvar = "This is now my value.<br />";
echo $yourvar;
$myvar = &$yourvar;
$myvar = "This is now my value.<br />";
echo $yourvar;
$formvar = $_POST["formvar"];
if ($_SESSION["loggedin"]){
echo "Proper login";
} else {
echo "You are not logged in.";
}
?>
PHP Superglobal Arrays
Array Description
$_COOKIE Contains keys and values set as browser cookies
$_ENV Contains keys and values set by the script"s shell context
$_FILES Contains information about uploaded files
$_GET Contains keys and values submitted to the script using the HTTP get method
$_POST Contains keys and values submitted to the script using the HTTP post method
$_REQUEST A combined array containing values from the $_GET, $_POST, and $_COOKIES superglobal arrays
$_SERVER Variables made available by the server
$GLOBALS Contains all global variables associated with the current script
PHP Variable Examples
<?php
$myvar = "foo"; /* Assigns the string "foo" */
$php5 = "is cool"; /* Correct Syntax */
?>
Valid and invalid variable names
$myvar Correct
$Name Correct
$_Age Correct
$___AGE___ Correct
$9 Incorrect ; starts with a number
$1Name Incorrect ; starts with a number
$Name1 Correct; numbers are fine at the end and after the first character
$_Name1 Correct
$Name"s Incorrect; no symbols other than "_" are allowed
valid statements
<?php
$number = "5"; # $number is a string
$sum = 15 + $number; # Add an integer and string to produce integer
$sum = "twenty"; # Overwrite $sum with a string.
?>