PHP/Language Basics/Variables

Материал из Web эксперт
Перейти к: навигация, поиск

A properly set-up PHP variable

   <source lang="html4strict">

<?php

   $myvar = 0; 
   $yourvar = "This is my value
"; $myvar = $yourvar; $myvar = "This is now my value.
"; echo $yourvar; $myvar = &$yourvar; $myvar = "This is now my value.
"; echo $yourvar;

?>

 </source>
   
  


Automatic Type Conversion

   <source lang="html4strict">

<?

   $mystring = "12";
   $myinteger = 20;
   print $mystring + $myinteger;

?>

 </source>
   
  


Calculate the variable name for simpler variables and use that name to access the value of that variable.

   <source lang="html4strict">

<?php $a0 = "This"; $a1 = "is"; $a2 = "a"; $a3 = "test"; for ($i = 0; $i < 4; $i++) {

 $var = "a$i";
 echo "${$var} ";

} ?>

 </source>
   
  


Numbers

   <source lang="html4strict">

<?

   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;

?>

 </source>
   
  


Numeric Data Types

   <source lang="html4strict">

<?php $a = 5; $b = $a / 2; echo "$a / 2 = $b\n";

$a = 6; $b = $a / 2; echo "$a / 2 = $b\n"; ?>

 </source>
   
  


Operating on variables

   <source lang="html4strict">

<?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; ?>

 </source>
   
  


PHP 5 variable functionality.

   <source lang="html4strict">

<?php

 $myvar = 0;
 $yourvar = "This is my value
"; $myvar = $yourvar; $myvar = "This is now my value.
"; echo $yourvar; $myvar = &$yourvar; $myvar = "This is now my value.
"; echo $yourvar; $formvar = $_POST["formvar"]; if ($_SESSION["loggedin"]){ echo "Proper login"; } else { echo "You are not logged in."; }

?>

 </source>
   
  


PHP Superglobal Arrays

   <source lang="html4strict">

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

 </source>
   
  


PHP Variable Examples

   <source lang="html4strict">

<?php

   $myvar = "foo";             /* Assigns the string "foo" */
   $php5 = "is cool";          /* Correct Syntax */

?>

 </source>
   
  


Valid and invalid variable names

   <source lang="html4strict">

$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

 </source>
   
  


valid statements

   <source lang="html4strict">

<?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.

?>

 </source>