Use of Data types in PHP


I think there is no use of data types in PHP as the PHP is a loosely type language.

It is true that the PHP is loosely typed, which means it calculates data types as data is assigned to each variable. But, this gives a mixed blessing. On the one hand, it means that variables can be used flexibly, holding a string at one point and an integer at another. On the other hand, this can lead to problems in larger scripts if you expect a variable to hold one data type when, in fact, it holds something completely different. You might have created code designed to work with an array variable, for example. If the variable in question contains a number value instead, errors might occur when the code attempts to perform array-specific operations on the variable.

Strings enclosed in Single or Double Quotation


Strings in PHP can be enclosed between the double quotation marks (“) or single quotation marks (‘). Where is the different in it.

There is a difference between double quotation marks and single quotation marks when used with strings.

Double quotation marks allow the parsing of variables. If you include a variable within double quotation marks the PHP engine substitutes the variable’s value, like so:

$name = “Jim”;
print “hello, $name”; // hello, Jim

If you use single quotation marks to enclose the same string, the variable is not substituted:

print ‘hello, $name’; // hello, $name

Double-quoted strings are also parsed for escape characters. Escape characters take on or lose special meaning when preceded by a backslash () character. Notable among these are n for a newline character, t for a tab, ” to print a double-quoted character within a double-quoted string, \ to print a backslash, and $ to print a dollar sign (so that it is not mistaken for the start of a variable).

As a rule of thumb, if you want a string to be output exactly as you typed it, you can use single quotation marks. This can help your code to run more quickly because the interpreter does not have to parse the string. If you want to take advantage of escape characters such as n and use variable substitution, you should use double quotation marks.

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.