Variable type


Apart from the getype() function in PHP, is there any more function, which can also return the variable type.

gettype() is a specialized tool. It does what it promises and returns a variable’s type.
var_dump() also tells you a variable’s type with that it also tells you contents it is holding. More than that, for complex types such as arrays and objects, var_dump() provides information about all the types contained within the variable, as well as about the variable itself.

$test = 5;
var_dump( $test );

This fragment gives us the following result:

int(5)

This tells us that the variable $test contains an integer and that the value of that integer is 5.

With var_dump() we did not need print to print the the result because the function prints its findings directly to the browser or command line.

but, with the gettype(), we do need to use the print statement:
print gettype(test);

settype() different from casting


We can change the type of variable with settype(). Then how is it different from casting?

The principle difference between settype() and a cast is the fact that casting produces a copy, leaving the original variable untouched.

In Casting:
$undecided = 3.14;
$holder = ( double ) $undecided;
print gettype( $holder ) ; // double
$holder = ( integer ) $undecided;
print gettype( $holder ); // integer
print ” — $holder<br />”; // 3

In settype():
$undecided = 3.14;
print gettype( $undecided ); // double
print ” — $undecided<br />”; // 3.14
settype( $undecided, int );
print gettype( $undecided ); // integer
print ” — $undecided<br />”; // 3

It is certainly not a procedure you will use often because PHP automatically casts for you when the context requires. However, an automatic cast is temporary, and you might want to make a variable persistently hold a particular data type.

Numbers typed in to an HTML form by a user are made available to your script as a string when entered in the text box. If you try to add two strings containing numbers, PHP helpfully converts the strings into numbers while the addition is taking place. So

“30cm” + “40cm”

produces the integer 70.
In casting the strings, PHP ignores the non-numeric characters. However, you might want to clean up your user input yourself. Imagine that a user has been asked to submit a number. We can simulate this by declaring a variable and assigning to it, like so:

$test = “30cm”;

As you can see, the user has mistakenly added units to the number. We can ensure that the user input is clean by casting it to an integer, as shown here:

$test = (integer)$test;
print “Your imaginary box has a width of $test centimeters”;

check the statement


Is this statement correct in PHP
print ( $name = “matt” );

Yes it is correct, it prints the string “matt” to the browser in addition to assigning “matt” to $name.

Objects Match on local PC but not on server


Hi, I am using PHP 4 on my PC, when I am comparing the two objects of same class, I am getting the resultant as true. But, when I am testing the same on the server of the cpwebhosting.net, the objects does not match. Why this is so?

PHP 4 implements === in a different way, comparing the properties of two objects and returning true if all properties match and both objects are instances of the same class. This behavior is quite different in the two versions of PHP, in that two different objects of the same type can have the same properties. In PHP 4, such objects would be held to be equivalent, whereas in PHP 5 the objects would not match.

constant to be case insensitive


Hi,

I have a developed a web based application using PHP, for using the application, user has to get logged in, I have defined the user as a constant value using
define().
Can we make it case-insensitive, as sometimes user types, JIM, jim, Jim, so it gives an error message, as the user does not match.

Well, define() optionally accepts a third boolean argument that determines whether the constant name should be case insensitive. By default, constants are case sensitive, but by passing true to the define() function you can change this behavior. So, if we were to set up our USER constant in this way

Define (“USER”, “JIM”, true);

we could access its value without worrying about case. So

print User;
print usEr;
print USER;

would all be equivalent.

To know the type of data a variable holds


Hi,

Why can it be useful to know the type of data a variable holds?

Often the data type of a variable constrains what you can do with it. You might want to ensure that a variable contains an integer or a double before using it in a mathematical calculation.

Obey any conventions when naming variables


Should I obey any conventions when naming variables?

You must try to make your code both easy to read and understable, for that you need to have variable names short and descriptive.

A variable named $f is unlikely to mean much to you when you return to your code after a month or so. A variable named $filename, on the other hand, should make more sense.

cram the entire the operator precedence table


Hi, Do we need to cram the entire the operator precedence table?

There is no reason why you shouldn’t, but it would save the effort for more useful tasks. By using parentheses in your expressions, you can make your code easy to read at the same time as defining your own order of precedence.

control structure’s test expression result in a Boolean value


Must a control structure’s test expression result in a Boolean value?

Ultimately, yes. However, in the context of a test expression, zero, an undefined variable, or an empty string is converted to false for the purposes of the test. All other values evaluate to true.

surround a code block in a control statement with brackets


Must I always surround a code block in a control statement with brackets?

If the code you want executed as part of a control structure consists of only a single line, you can omit the brackets. Retaining the brackets for single-line control statements can also help guard against bugs as new lines are added to the block over time.