Data Types in PHP

PHP is not a very strict language when it comes down to data types.
Strings:


$string = "This is a string";

Strings are values that contain characters. This can include basically everything you can type on your keyboard.

Integers:


$string = 1;

Integers are values that just contain numbers. Notice in the example above that there are no quotes?
Well, you can write an integer like that, or like this:


$string = "1";

PHP automatically converts numbers contained in a string with no other characters into a integer. The only time you need to be careful of this is when you are using operators (I will discuss that in another post).

Boolean:


$string = true;
$string2 = false;

Booleans are values that contain either true or false. They can be used in if statements like:


$string = true;
if ($string)//checks if $string is true
 {
 echo '$string is true';
 }
else
 {
 echo '$string is false';
 }

Hope that helps you understand data types in PHP better. Obviously I have just scratched the surface in my explanations.
Feel free to comment and ask any questions 😀

This entry was posted in Coding and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Data Types in PHP

  1. Zac Davis says:

    Great tip. I’m just getting into PHP. And you explain things in an easy to understand way. I hope you keep offering more great tips like theses.

  2. papa_face says:

    Thank you for the kind words 😀
    I will definately be producing more of these help articles 🙂

Leave a Reply