PHP Data Types Explained
PHP like all programming languages store information in variables. These types of information come in many flavors and are called "data types". Each data type has it's unique purpose and guidelines. If you've ever taken an algebra class you are familiar with the concept of variables as I am sure you were racking your brain to find the value of "x". In computer science things are a bit more simple as the programmer is responsible for assigning variables with a type of data, a data type... so to speak.
PHP variables, Show me the $. All PHP variables begin with "$".
Example of a PHP variable
$x = 1;
Here's a few rules to abide by (according to W3Schools)
•A variable name must start with a letter or an underscore "_"
•A variable name can only contain alpha-numeric characters and underscores
(a-z, A-Z, 0-9, and _ )
•A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
PHP has 8 data types
-integers -floating point numbers -strings -booleans -arrays -objects -resouces -null PHP is a great because it's a "loosely-typed" language. Meaning variables can change as the code demands it. Although, it is very important that you fully understand the types of data to program in PHP. Let's take a moment and explain each data type.
1. Integers (int)
An Integer is a whole number, nuff said. Integers do not allow fractions. Int examples are –32, 0, 1, 55, or 5438. Integers do allow Octal or Hexidecimal based numbers but I prefer the common representation. In PHP an Integer is a number between -2,147,483,648 and +2,147,483,647. PHP also offers a function to find whether a variable is an integer or not. This function is is_integer().
2. Floating Point Numbers (float)
So unlike Integers, Floating Point numbers do have a fractional component. In fact they get there name from the decimal point itself. In other languages you may have seen Floating Point numbers referred to as Doubles. Float examples are 0.32, -1.3, 23.3, or 34.43. PHP also offers a function to find whether a variable is an Floating Point Number. This function is is_float().
In PHP, strings are simply a string of text enclosed in double or single quotes. In web development you will work a lot with strings. The use of double quotes in strings allow the programmer to call a function within the string itself, without having to write and escape sequence.
Examples of Strings with double and single quotes $myVar = "Double Quotes"; // assign a value to $myVar echo $myVar; // outputs 'Double Quotes' echo "I can write $myVar"; // outputs 'I can write Double Quotes' // but with single quotes echo 'I can write $myVar': // outputs 'I can write $myVar' Speaking of "quotes", as soon as you use quotes the data type becomes a string. Even if you place numbers between the quotes, like "1234".
Common escape sequences \n creates a new line \r creates a carriage return \\ creates a single slash \" creates a double quote \' creates a single quote You may use the is_string() function to test whether something is a string.
Booleans only have two values, true and false. If a different data type is converted from the conditional then 0 becomes false and 1 becomes true. The following data type values will resolve false. -The keyword literal false. -The integer 0. -The floating-point number 0.0. -The empty string (""). -The string "0" (zero). -An object with no values or methods. -The null value.
PHP Arrays store collections of data. There are two types of Arrays, Indexed Arrays, which store items with a numeric index and Associative Arrays which store items with named indexes.
Arrays may be reference using the following: $arrayTitle[index];
Assigning value to Array Items $arrayTitle = "zero"; $arrayTitle = "one"; $arrayTitle = "two"; $arrayTitle = "three"; If you want to find out if a variable contains an array you can use the is_array() function. Individual elements of an array can be any data type.
With the introduction of PHP 5 came Object Oriented Programming. Programmers quickly realized the power of OOP and wrote it in, and they were smart to do so. Objects can be thought about as things in life... nouns. A car is an object. A door is an object. Objects store not just data but how to process that data. The elements within an object as called it's properties. If your object is a car, a property may be that it is red. In other languages you may see objects referred to as classes and properties as attributes. How objects process data is in the form of functions, sometimes called methods. You have created a red car, a function may be for it to "drive" or "start". is_object() function to test whether a variable is on object instance.
Resources are just that, a common resource call would be connecting to a database. This will be explained in a future topic.
A data type that has no value assigned to it is considered Null. That's the basics of variables and data types. There is much more to be said within individual data types and how they are used. I'll try and expand on each one in future posts. overandout Jamie Rushad Gros.