variables must have a "type". that's exactly what it sounds like -- if a variable is some information, the type is what kind of information it is. for example, the difference between a piece of text and a number.
some languages force you to specify what type each piece of information is but php automatically assigns types in most cases. you can force it to use a particular type and, in some cases, you have to be clear on it to avoid problems. you can also convert variables from one type to another.
the types we've already looked at are string and int. string is text. int stands for integer and is, as it says, an integer -- a number without a decimal. it can be positive, negative or zero.
$variable = "hello"; automatically assigns this to be a string.
$variable = 20; automatically assigns this to be int.
there is one other type of number, float, a floating-point number. that's a number with a decimal. for a whole variety of reasons, this type is best avoided unless you understand the possible issues with mathematical computation using floating-point arithmetic. if a number has a decimal, it calls it float. otherwise it is int.
$variable = 3.951; automatically assigns this to be float.
the other two simple types are null, when something simply has no value at all, and bool, a "boolean", which means it is either true or false.
$variable = null; automatically assigns this to be null.
$variable = true; automatically assigns this to be bool.
there is an unlimited number of types because you can create your own but the other two built-in ones you will encounter frequently are array and object. both of these are just collections of information of other types.
$variable = ["abc", "def", "ghi"];
this creates an array called $variable containing three strings.
$variable = [17, 32, 80, 9, 26];
this creates an array called $variable containing three ints.
note that strings, even inside arrays, still get wrapped in "" while numbers don't.
if you want to write something, you are already familiar with the most common way to do it, print_r. if you want more information, however, you can use var_dump. the difference between these is that print_r will tell you what is stored inside but var_dump will tell you that as well as what type of information it is.
print_r("hello");
returns hello.
var_dump("hello");
returns string(5) "hello". in other words, a string 5 characters long whose value is hello. this is useless in a final program but often helps when trying to find the source of an error.
we will look at writing our own functions soon but, while we're on the subject of types of information, we need to talk about functions. a function is a section of the code that does something. that might sound vague but you have already encountered several of them. you can think of a function as a "command". sometimes it does an action. sometimes it returns a value.
the two you have already seen are print_r and var_dump. they both take an input and write it where the user can see it. they do it slightly differently, which is why there are two different names for them.
the syntax to run a function is the name of the function followed by any information being passed to the function between (). the reason we're mentioning this now is that we're about to look at a bunch of built-in functions for doing common tasks.
i mentioned earlier that we will be using print_r instead of echo when both write information to the screen. there are slight differences in how they work but the important one to be aware of now might be obvious. print_r is a function but echo is not. that means that everything we learn about using functions applies to print_r but echo behaves somewhat differently and that can cause problems. there are, for example, some things that can be done with print_r but not echo but everything echo can do can be done using print_r.
one of the most common things that needs to be done inside a program is modifying a string of text. many php guides start with built-in functions to change case. i will do the same but with a significant difference. the commonly-used functions are strtolower and strtoupper, which immediately break when you use them with some accented or foreign-language characters. this is the source of endless headaches for beginners and even many advanced php developers. so we will skip this problem and use the function that works for all characters.
this also gives us the opportunity to talk about built-in constants and multiple parameters being passed to functions. the function mb_convert_case can take more than one piece of information at a time and one of them is usually a built-in constant. these are separated by ,. we have talked about how to set a constant but this is an example of some of the built-in ones.
$output = mb_convert_case("hello there, everyone!", MB_CASE_TITLE);
variable $output is Hello There, Everyone!.
while constants can be any combination of letters and numbers, all the built-in ones are all-caps. there are various possible options for this function but the ones you should remember for the moment are MB_CASE_LOWER, MB_CASE_UPPER and MB_CASE_TITLE.
as you already know, php ignores whitespace in the code but not inside strings. as a result, if you collect text from a user, it will often start or end with extra spaces that can become messy or cause problems. this is an easy issue to avoid, however, because php includes functions to remove whitespace from the end, beginning or both.
$input = " some text is here ";
$one = ltrim($input); // "some text is here "
$two = rtrim($input); // " some text is here"
$three = trim($input); // "some text is here"
ltrim trims whitespace from the left. rtrim trims whitespace from the right. trim trims whitespace from both.
it is often necessary to take something inside a string and either remove it or replace it with something else. thankfully, there are several built-in functions to do just that.
if you know exactly what you want to remove or replace, you can use str_replace to achieve either.
$input = "may i have a banana?";
$input = str_replace("banana", "grapefruit", $input);
print_r($input);
returns may i have a grapefruit?.
the syntax is str_replace(change this, to this, in this).
to remove something, just use an empty string as the "to this". in other words, replace it with nothing.
$input = str_replace("!", "", "banana!"); // "banana"
we often don't know exactly what we want to replace, though. that's where "regular expressions" come in. they will come back again and again but this is a simple introduction. a regular expression is a search pattern. it looks for predictable things in text instead of exact matches.
imagine you want to hide any numbers in a string.
$input = "my social security number is 123-45-6789."
$input = preg_replace("/\d/", "X", $input); // "my social security number is XXX-XX-XXXX."
\d is the search parameter for any digit. preg_replace works the same as str_replace but it takes a regular expression instead of a normal string.
there are two ways to combine the results of multiple functions. the best solution is usually to run them one after another.
$input = " banana! "; // " banana! "
$input = str_replace("!", "", $input); // " banana "
$input = trim($input); // "banana"
$input = mb_convert_case($input, MB_CASE_UPPER); // "BANANA"
the alternative is to string them together. this can get very messy but can be useful in some cases.
$input = mb_convert_case(trim(str_replace("!", "", " banana! ")), MB_CASE_UPPER); // "BANANA"
of course, the first option is far more readable and definitely recommended in almost all situations. the second is included as both a demonstration of how messy chaining functions can be and an example of how function-chaining works in situations where it is appropriate.
$input = " orange ";
print_r(trim($input));
returns orange and is a much more common and friendly use of chaining.
the best rule when it comes to coding -- and this will be repeated over and over -- is that it should be readable. if you can't immediately tell what is happening in each line, there is probably a better way to write it.
php has the ability to function as an object-oriented language. sort of. most guides talk about how to do that and praise object-oriented programming. i'm not going to do that. this is not the place for a discussion of whether object-oriented programming has a valid place in the development world. my view is that it doesn't but there are places where it might have some usefulness.
php is not one of those places. it is to be avoided like the black death.
that is not the same as avoiding the "object" data type, which is, in php, really just a slightly different type of array. in practice, though, it is far easier and more efficient to use normal arrays in almost all cases.