Belajar PHP Variabel Dalam PHP

Dalam rangka mempelajari PHP, pertama-tama harus diketahui dulu apa itu sebenarnya yang dinamakan variabel dan bagaimana penggunaannya

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 ASCII characters from 127 through 255 (0x7f-0xff).

Tip: You may also want to take a look at the Appendix S.

For information on variable related functions, see the Variable Functions Reference.

= 'Bob';
$Var = 'Joe';
"$var, $Var"; // outputs "Bob, Joe"#pendtag# #pstarttag#$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228.

In PHP 3, 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.

As of PHP 4, PHP 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:

= 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
$foo; // $foo is altered too.

One important thing to note is that only named variables may be assigned by reference.

= 25;
$bar = &$foo; // This is a valid assignment.
$bar = &(24 * 7); // Invalid; references an unnamed expression.#pendtag# #pstarttag#function test()
}#pendtag# #pstarttag#
$bar = &test(); // Invalid.

It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type – FALSE, zero, empty string or an empty array.

Example 12-1. Default values of uninitialized variables

echo ($unset_bool ? "true" : "false"); // false
$unset_int += 25; // 0 + 25 => 25
echo $unset_string . "abc"; // "" . "abc" => "abc"
$unset_array[3] = "def"; // array() + array(3 => "def") => array(3 => "def")

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

Pusing kan? ehueheuheuehe :) postingan ini perkenalan terhadap PHP aja kok..

Tags: , ,


# Full time business development member, part time philosopher, amateur web programmer, Ubuntu lover, and guitar player.

Artikel Terkait

9 tanggapan untuk “Belajar PHP Variabel Dalam PHP”

  1. May 10th, 2009 at 03:13 | #1

    tes dulu,… penghuninya ada nggak yah,..???

  2. May 10th, 2009 at 03:52 | #2

    Aku nggak bisa koment disini,?@#$%&>>>>>>>….

  3. May 10th, 2009 at 23:41 | #3

    php? kok gua bacanya pmp ya? jadi laper.. pengen makan pempek megaria…ntar sore kesana ahhh

  4. May 13th, 2009 at 03:44 | #4

    wahhh.. bahasanya inggris,, kapan2 posting yg pake translate nya aja dong…hehe…

  5. May 13th, 2009 at 15:39 | #5

    aku nggak ngerti ini nih, cuman ngaoment ajah selagi masih bisa ngoment…hehehehehhe

  6. aku
    December 15th, 2009 at 02:18 | #6

    copy paste ya bro… :roll:

  7. May 10th, 2009 at 14:45 | #8

    hauhauhuahua kabur giihhh.. :))

  8. May 11th, 2009 at 15:53 | #9

    PMP?? itu kan pelajaran waktu SD..

Silakan Beri Komentar