Книга: Fedora™ Unleashed, 2008 edition

Variables

Variables

All variables in PHP start with a dollar sign ($). Unlike many other languages, PHP does not have different types of variable for integers, floating-point numbers, arrays, or Booleans. They all start with a $, and all are interchangeable. As a result, PHP is a weakly typed language, which means you do not declare a variable as containing a specific type of data; you just use it however you want to.

Save the code in Listing 27.1 into the script fedora1.php.

LISTING 27.1 Testing Types in PHP

<?php $i = 10;
 $j = "10";
 $k = "Hello, world";
 echo $i + $j;
 echo $i + $k;
?>

To run that script, bring up a console and browse to where you saved it. Then type this command:

$ php fedora1.php

If PHP is installed correctly, you should see the output 2010, which is really two things. The 20 is the result of 10 + 10 ($i plus $j), and the 10 is the result of adding 10 to the text string Hello, world. Neither of those operations is really straightforward. Whereas $i is set to the number 10, $j is actually set to be the text value "10", which is not the same thing. Adding 10 to 10 gives 20, as you would imagine, but adding 10 to "10" (the string) forces PHP to convert $j to an integer on the fly before adding it.

Running $i + $k adds another string to a number, but this time the string is Hello, world and not just a number inside a string. PHP still tries to convert it, though, and converting any nonnumeric string into a number converts it to 0. So, the second echo statement ends up saying $i + 0.

As you should have guessed by now, calling echo outputs values to the screen. Right now, that prints directly to your console, but internally PHP has a complex output mechanism that enables you to print to a console, send text through Apache to a web browser, send data over a network, and more.

Now that you have seen how PHP handles variables of different types, it is important that you understand the selection of types available to you — see Table 27.1.

TABLE 27.1 Data Types in PHP

Type Stores
integer Whole numbers; for example, 1, 9, or 324809873
float Fractional numbers; for example, 1.1, 9.09, or 3.141592654
string Characters; for example, "a", "sfdgh", or "Fedora Unleashed"
boolean True or false
array Several variables of any type
object An instance of a class
resource Any external data

The first four can be thought of as simple variables, and the last three as complex variables. Arrays are simply collections of variables. You might have an array of numbers (the ages of all the children in a class); an array of strings (the names of all Wimbledon tennis champions); or even an array of arrays, known as a multidimensional array. Arrays are covered in more depth in the next section because they are unique in the way in which they are defined.

Objects are used to define and manipulate a set of variables that belong to a unique entity. Each object has its own personal set of variables, as well as functions that operate on those variables. Objects are commonly used to model real-world things. You might define an object that represents a TV, with variables such as $CurrentChannel (probably an integer), $SupportsHiDef (a Boolean), and so on.

Of all the complex variables, the easiest to grasp are resources. PHP has many extensions available to it that allow you to connect to databases, manipulate graphics, or even make calls to Java programs. Because they are all external systems, they need to have types of data unique to them that PHP cannot represent by using any of the six other data types. So, PHP stores their custom data types in resources — data types that are meaningless to PHP but can be used by the external libraries that created them.

Оглавление книги


Генерация: 0.051. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз