Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP Variables and Variable Functions

case sensitive variable type

  • Please log in to reply
7 replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 04 September 2009 - 05:57 AM

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:

$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:

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:

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:

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:

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:

$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:

$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:

$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:

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

Output:

Not set.


  • 1

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 05 September 2009 - 06:06 AM

Not bad, +rep!
  • 0

#3 Termana

Termana

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 971 posts

Posted 09 September 2009 - 05:40 PM

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 :D
  • 0

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#4 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 10 September 2009 - 04:26 AM

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
  • 0

#5 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 10 September 2009 - 04:44 AM

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

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

echo ${$myvar};
echo ${'4variable'};


Results:
This is a test!This is a test!

  • 0

#6 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 10 September 2009 - 04:49 AM

Nice test :P

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

#7 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 10 September 2009 - 04:51 AM

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

#8 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 10 September 2009 - 06:36 AM

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

<?PHP
$name = 3;
$$name = "Hello World";
echo $$name;

  • 0





Also tagged with one or more of these keywords: case sensitive, variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download