Saturday, July 31, 2010

Variables

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Note: For our purposes here, a letter is a-z, A-Z, and the bytes from 127 through 255 (0x7f-0xff).
Note: $this is a special variable that can't be assigned.
Tip
See also the Userland Naming Guide.
For information on variable related functions, see the Variable Functions Reference.
$var 'Bob';$Var 'Joe';
echo 
"$var$Var";      // outputs "Bob, Joe"
$4site 'not yet';     // invalid; starts with a number$_4site 'not yet';    // valid; starts with an underscore$täyte 'mansikka';    // valid; 'ä' is (Extended) ASCII 228.?>
By default, 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 the chapter on Expressions.
PHP also offers another way to assign values to variables: assign by reference. 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.
To assign by reference, simply prepend an ampersand (&) 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:
$foo 'Bob';              // Assign the value 'Bob' to $foo$bar = &$foo;              // Reference $foo via $bar.$bar "My name is $bar";  // Alter $bar...echo $bar;
echo 
$foo;                 // $foo is altered too.?>

Types

Types
Jeffrey

The Object (compound) Type

Like every programming language, PHP offers the usual basic primitive types which can hold only one piece of data at a time (scalar). I am particularly fond of the "object" type (compound) because that allows me to group many basic PHP types together, and I can name it anything I want.

class Person
{
 
$firstName;                   // a PHP String
 
$middleName;                  // a PHP String
 
$lastName;                    // a PHP String
 
$age;                         // a PHP Integer
 
$hasDriversLicense;           // a PHP Boolean
}
?>

Here, I have grouped several basic PHP types together, (3) Strings, (1) Integer, and (1) Boolean... then I named that group "Person". Since I used the proper syntax to do so, this code is pure PHP, which means that if you run this code, you would have an extra PHP "type" available to you in your scripts, like so:

$myAge = 16;                    // a PHP Integer - always available
$yourAge = 15.5;                // a PHP Float   - always available
$hasHair = true;                // a PHP Boolean - always available
$greeting = "Hello World!"      // a PHP String  - always available

$person = new Person();         // a PHP Person  - available NOW!
?>

You can make your own object types and have PHP execute it as if it were part of the PHP language itself. See more on classes and objects in this manual at: http://www.php.net/manual/en/language.oop5.php
arjini at gmail dot com

Note that you can chain type castng:

var_dump((string)(int)false); //string(1) "0"
shahnaz khan

if we use gettype() before initializinf any variable it give NULL
for eg.

$foo;
echo
gettype($foo);
?>

it will show

NULL

Basic Syntax

Basic syntax
mattsch at gmail dot com


Less is more.  The shortest and easiest way to deal with the xml tag problem assuming short tags is enabled and you don't care to listen to people who want you to always use the full php tag is this:

<?xml version="1.0" encoding="utf-8"?>

KOmaSHOOTER at gmx dot de


you also can use this kind of syntax:

if( condition ): ?>
 
else: ?>
 
endif; ?>

Tona at spikesource dot com

Jascam: Try to find more resourceful information to make your point. Your lack of ability to understand more complex concepts is not enough to diminish such a popular language as PHP. Note also that php is not replacing html but complementing it.

madman


As rosswilliams mentioned;  The example breaking in and out of the PHP tags doesn't "work as expected".  The manual and some comments mention that PHP "simply starts outputting whatever it finds..." any time a closing tag is encountered.  It would make sense to say instead - "...unless PHP is in the middle of a conditional statement; in which case it will only output the relevant block of HTML."

Some have said that using the 'echo' command is cleaner and more  efficient.  However, as the manual points out, the method of breaking out of PHP is more efficient when dealing with large sections of HTML.  Because there is less work for the parser.

Geekman at Textbook Torrents dot com

Regarding the comment by rosswilliams at advocacytechnologies dot org:

Your suspicion is correct. The following all behave exactly the same:


// output the answer by escaping
if ($true_or_false) {
   
?>
    The value of $true_or_false is true.

    } else {
   
?>
    The value of $true_or_false is false.

    }

// use echo to do the same thing - more efficient and easier to read in my opinion
if ($true_or_false) {
    echo
'The value of $true_or_false is true.';
} else {
    echo
'The value of $true_or_false is false.';
}

// use ? : operators on entire string
echo ($true_or_false) ? 'The value of $true_or_false is true.' : 'The value of $true_or_false is false.';

// use ? : operators only on the pertinent bit, to save space
echo 'The value of $true_or_false is ' . (($true_or_false) ? 'true' : 'false') . '.';

?>