+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP Variables and Variable Functions

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    PHP Variables and Variable Functions

    PHP Variables

    In PHP, variable names are case-sensitive. These variables: $test, $Test, $TEST, $tesT all are different variables. Unlike C languages, you do not have to declare a variable before you use it. If you use a variable that you have not created in a script, then PHP will just create a new variable for you. This may make it harder to find your error.

    Creating a variable

    PHP variables start with a dollar sign. The first character must be a letter. Other than that, you can use any letter or number. The variable name can be as long and as short as you want.

    Examples:

    Code:
    $text = "test";
    $text = 5;
    Another thing to note, is that you can change the type of variable. This differs from Java and C++. This can also cause a source of error if you are not careful. First the $text variable was a string variable, and then it becomes an integer variable.

    Constants

    A constant variable is a variable that once defined, can never be changed. I like to put all my constants in strict upper case. This makes it easier to find. A useful example of constants is database access information.

    Once this information is set, you wouldn't want to change it.

    We create constants using the define function.

    Examples:

    Code:
    define("DB_HOST","localhost");
    define("DB_USER","root");
    define("DB_PASS","pass");
    The first parameter is the name of the constant. The second parameter is the value of the parameter. So I could connect to a database like this:

    Code:
    mysql_connect(DB_HOST,DB_USER,DB_PASS);
    You can use them just like any other variable. The only thing is you can't change them.

    If I try to change the variable, I get an error.

    Example:

    Code:
    DB_HOST = "Test";
    The error I get is:

    Parse error: parse error in C:\wamp\www\PHP\constants.php on line 54
    This is more than useless because I don't have errors set very well in my php.ini file. All I know is that I have an error on line 54. This is referring to where I try to change the value of the constant.

    The third optional parameter allows me to specify if the constant is case sensitive or not. By default DB_HOST and db_host are the same constants. If I pass false as the third parameter, this makes DB_HOST and db_host both two different constants.

    Example:

    Code:
    define("DB_HOST2","localhost",false);
    
    echo DB_HOST2 . " " . db_host2
    When I run this code, I get this warning:

    Notice: Use of undefined constant db_host2 - assumed 'db_host2' in C:\wamp\www\PHP\constants.php on line 56
    You don't have to pass true. The function assumes a default value of true, if you don't pass a third argument.

    Checking Variable Type

    PHP provides method is_int, is_string, is_array and so on for you to assess what type of data a variable is holding currently. Note that at one point in the string is_int can return true, and return false later. Variable types can change.

    Example:

    Code:
    $x = "test";
    $y = 5.5;
    $b = false;
    $test = 5;
    
    if (is_int($x)) {
    	echo "$$x is an int.<br />\n";	
    } else {
    	echo "$$x is a " . gettype($x) . "<br />\n";	
    }
    This code tests the what type of data $x is holding. I use two $ signs as to avoid printing the content of the variable. The gettype method returns the type of the data.

    So this code outputs:

    $x is a string.
    So if tested $x with is_string, the method is_string would return true.


    Isset, unset and empty

    The isset method returns true if the variable has been set. This is similar to the empty function but they are not the same. The empty function returns true if a variable has not been assigned a value or has a false value. PHP considers "" (empty string), false, and 0 to be empty. Even though the variable has been assigned a value.

    Example:

    Code:
    $x = 0;
    	
    if (empty($x)) {
    	echo "Empty.";	
    } else {
    	echo "Not empty.";	
    }
    Output:

    Empty.
    However, even with a false value, isset will return true. This function simply checks if the variable has a value.

    Example:

    Code:
    $x = 0;
    	
    if (isset($x)) {
    	echo "Set.";	
    } else {
    	echo "Not set.";	
    }
    Output:

    Set.
    The unset function removes a variable, so that isset will return false.

    Example:

    Code:
    unset($x);
    if (isset($x)) {
    	echo "Set.";	
    } else {
    	echo "Not set.";	
    }
    Output:

    Not set.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: PHP Variables and Variable Functions

    Not bad, +rep!

  4. #3
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: PHP Variables and Variable Functions

    Great tutorial, +rep

    Edit: Or not... I have to spread some rep around before I can give some to you. But, if I could rep you, I would
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: PHP Variables and Variable Functions

    Great tut buddy! + Rep!


    PHP variables start with a dollar sign. The first character must be a letter. Other than that, you can use any letter or number. The variable name can be as long and as short as you want.
    Underscores can be the first letter as well.
    PHP: Basics - Manual

  6. #5
    Jordan Guest

    Re: PHP Variables and Variable Functions

    Even though the manual says you can't, you can use numbers as the first character using variable variables:

    Code:
    <?php
    $myvar 
    '4variable';
    ${
    '4variable'} = 'This is a test!';

    echo ${
    $myvar};
    echo ${
    '4variable'};
    Results:
    Code:
    This is a test!This is a test

  7. #6
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: PHP Variables and Variable Functions

    Nice test

    Even if you can it probably is not the best idea to do so.

  8. #7
    Jordan Guest

    Re: PHP Variables and Variable Functions

    I can't imagine a reason for ever wanting to use such code.

  9. #8
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: PHP Variables and Variable Functions

    Why do you think it works? Maybe it encodes it or something...

    Code:
    <?PHP
    $name 
    3;
    $
    $name "Hello World";
    echo $
    $name;

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 10-24-2011, 08:12 AM
  2. Beginner Ruby, Variables and Functions
    By RhetoricalRuvim in forum Ruby on RailsTutorials
    Replies: 0
    Last Post: 08-27-2011, 04:56 PM
  3. Php eval, functions and variables
    By webcodez in forum PHP Development
    Replies: 1
    Last Post: 05-15-2010, 12:59 AM
  4. Functions With a Variable Number of Arguments
    By dargueta in forum C Tutorials
    Replies: 6
    Last Post: 10-05-2009, 04:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts