Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-release > by-pkgid > ebb1914cf182a88528b4547490db1dd8 > files > 1688

kdewebdev-quanta-doc-3.5.9-2mdv2008.1.x86_64.rpm

<HTML
><HEAD
><TITLE
>Variables</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Language Reference"
HREF="langref.html"><LINK
REL="PREVIOUS"
TITLE="Type juggling"
HREF="language.types.type-juggling.html"><LINK
REL="NEXT"
TITLE="Predefined variables"
HREF="language.variables.predefined.html"></HEAD
><BODY
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.types.type-juggling.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.variables.predefined.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="language.variables"
>Chapter 7. Variables</A
></H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="language.variables.html#language.variables.basics"
>Basics</A
></DT
><DT
><A
HREF="language.variables.predefined.html"
>Predefined variables</A
></DT
><DT
><A
HREF="language.variables.scope.html"
>Variable scope</A
></DT
><DT
><A
HREF="language.variables.variable.html"
>Variable variables</A
></DT
><DT
><A
HREF="language.variables.external.html"
>Variables from outside PHP</A
></DT
></DL
></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.variables.basics"
>Basics</A
></H1
><P
>&#13;    Variables in PHP are represented by a dollar sign followed by the
    name of the variable. The variable name is case-sensitive.
    <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp; 
  2&nbsp;$var = "Bob";
  3&nbsp;$Var = "Joe";
  4&nbsp;echo "$var, $Var"; // outputs "Bob, Joe"
  5&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    In PHP3, variables are always assigned by value. That is to say,
    when you assign an expression to a variable, the entire value of
    the original expression is copied into the destination
    variable. This means, for instance, that after assigning one
    variable's value to another, changing one of those variables will
    have no effect on the other. For more information on this kind of
    assignment, see <A
HREF="language.expressions.html"
>Expressions</A
>.
   </P
><P
>&#13;    PHP4 offers another way to assign values to variables:
    <I
CLASS="emphasis"
>assign by reference</I
>. This means that the new
    variable simply references (in other words, "becomes an alias for"
    or "points to") the original variable. Changes to the new variable
    affect the original, and vice versa. This also means that no
    copying is performed; thus, the assignment happens more
    quickly. However, any speedup will likely be noticed only in tight
    loops or when assigning large arrays or objects.
   </P
><P
>&#13;    To assign by reference, simply prepend an ampersand (&#38;) to the
    beginning of the variable which is being assigned (the source
    variable). For instance, the following code snippet outputs 'My
    name is Bob' twice:

    <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;&#60;?php
  3&nbsp;$foo = 'Bob';              // Assign the value 'Bob' to $foo
  4&nbsp;$bar = &#38;$foo;              // Reference $foo via $bar.
  5&nbsp;$bar = "My name is $bar";  // Alter $bar...
  6&nbsp;echo $foo;                 // $foo is altered too.
  7&nbsp;echo $bar;
  8&nbsp;?&#62;
  9&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    One important thing to note is that only named variables may be
    assigned by reference.
    <DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;&#60;?php
  3&nbsp;$foo = 25;
  4&nbsp;$bar = &#38;$foo;      // This is a valid assignment.
  5&nbsp;$bar = &#38;(24 * 7);  // Invalid; references an unnamed expression.
  6&nbsp;
  7&nbsp;function test() {
  8&nbsp;   return 25;
  9&nbsp;}
 10&nbsp;
 11&nbsp;$bar = &#38;test();    // Invalid.
 12&nbsp;?&#62;
 13&nbsp;     </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.types.type-juggling.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.variables.predefined.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Type juggling</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="langref.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Predefined variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>