As we have already learnt about using XAMPP and other tools, PHP open and close tags, phpinfo function, printing text on screen, adding comments in PHP files and some conversation on what does the server do, it’s now time for learning the various types of data. In this post, we will talk about variables, strings and numbers.
Variables
Simple definition of a variable is that it is a symbolic representation of a value. But as its name says, it can change or vary. In PHP, variables are going to
- Start with a $ (dollar) sign.
- They can be then followed by a letter or underscore.
- They can contain letters, underscores, numbers and dashes.
- There can NOT be any spaces in them.
- They are case-sensitive.
You can see the following examples of variables. Also see the comments with them for the explanation.
$devil
$Devil //see the capital D
$devilsWorkshop //can contain capital letters in-between
$devils_workshop //can contain underscores
$devils-workshop //can contain dashes, but I recommend not to use these
$D3v1ls_W0rk-sh0p //can contain numbers, capital letters, underscores, dashes
$_devil //can contain underscore in the beginning, not recommended as PHP already uses single underscore itself to define a certain type of variable
$__devil //can contain as many underscores, not recommended as it is difficult to make out just by seeing that how many underscores are there
Now, as we have learnt about the types of variables we can have, we must do some experiment. Here is one!
<?php
$devil = 1; //assigning a number to a variable, numbers don't need to be wrapped with quotes
echo $devil."<br />"; //note that we can put a period and merge two strings
$devilvar = "Hello World"; //settings a variable
$devilVar = "Hello World!!"; //note here that the "V" is capital and the variables are case-sensitive, so $devilvar is different from $devilVar
echo $devilVar."<br />"; //it will echo back "Hello World!!", not "Hello World" as the variables are case sensitive
$devil = 2; //we can reset a variable
echo $devil; //the new value which is "2" will be echoed.
?>
Here is an online demo.You can view the highlighted source here.
We will do the same thing which we did before, go to htdocs and create a new file named “variables.php”. Paste this code there. And access it on http://localhost/variables.php.
You will understand everything by reading the comments which are written in the code.
Strings
We have been using strings everywhere without even knowing about that. For example, “Hello World” is a string. Let us learn more about them! Here is a code, read the comments in the code too (actually that’s the explanation).
<?php
echo "Hello World<br />"; //we can wrap strings within double quotes and also put HTML code in them
echo 'Hello World<br />'; //we can also wrap them with single quotes
$devil = "Welcome in the World"; //we can assign strings to variables
echo $devil."<br />"; //we can echo variables, while we also merge HTML with them
echo "$devil of Devils!<br />"; //we can put the variable within the double quotes - not recommended as if you put $devilof, it will try to search for a variable called $devilof
echo "{$devil} of DevilsWorkshop!<br />"; //We can wrap the variable with curly brackets and put it in within double quotes - Recommended
echo '$devil of Devils!<br />'; //this technique doesn't works with single quotes, it will echo "$devil of Devils!" as it doesn't try to parse any variables within single quotes
?>
Here is an online demo.You can view the highlighted source here.
So, we came to a conclusion that we should wrap all variables with curly braces and always use double quotes (Well, it all depends on you, it is not necessary. We can still use periods to put variables in single quote strings).
String Functions
Well, I can’t cover all the string functions, but I will cover the basic ones. You can see the exhaustive list of functions on PHP.net and can take more reference from there. Here is a code by which you can understand it better!
<?php
$devil = "Devils"; //assigning a string to a variable
$workshop = " Workshop"; //notice the space in the beginning
$tut = "php tutorials"; //assigning another string to a variable
$dw = $devil; //the value of $devil is now also the value of $dw
$dw .= $workshop; //we can also merge 2 strings like this, notice the period with equals to sign (".=")
echo $dw."<br />"; //echoing the merged string with HTML
// Below are some functions which we can apply, refer to PHP.net for more functions
echo strtolower($dw)."<br />"; //this will make all the letters of the string to lowercase and echo the string. Note that a function can be used with another function.
echo strtoupper($dw)."<br />"; //this will make all the letters of the string to uppercase and echo the string.
echo ucfirst($tut)."<br />"; //this will make the first letter of the string capital. Note - we are using $tut here, not $dw
echo ucwords($tut)."<br />"; //this will make the first letter of each word in the string to a capital letter. Note - we are using $tut here, not $dw
echo strlen($dw)."<br />"; //this will count the number of letters in the string and echo it
echo $trimmed = $devil.trim($workshop)."<br />"; //See here, that I have made a new variable and also echoed it. The new variable contains $devil as it is and trim function is applied to $workshop which will remove all the whitespaces (spaces, tabs etc.) from the beginning and ending.
echo strstr($dw, "W")."<br />"; //this function is string in a string. It finds a string in a string and returns the string which is after the string we are trying to find with the string we are trying to find. So, here $dw is the string and "W" is the string which we are finding. It should echo "Workshop" as the echo function is also there.
echo str_replace("Workshop", "Head", $dw)."<br />"; //this function will replace the word Workshop with Head in the string $dw. So, it should echo back "Devils Head"
echo str_repeat($dw." ", 3)."<br />"; //it will repeat the string 3 times (see the last value entered in the function. Also note that I added a space after $dw within the function.
echo substr($dw, 7, 4)."<br />"; //it will echo 4 characters which are after 6 letters in the string (see the 3rd and 2nd value of the function)
echo strpos($dw, "W")."<br />"; //it will echo the position of the letter "W"
?>
Here is an online demo. You can view the highlighted source here. Hope you understood this long code. If you have any doubts, then please ask in the comments section.
Numbers
As we have already talked about variables, strings and strings functions, we will now talk about numbers. You know what are numbers.. don’t you? No?
So let us try PHP doing some maths for us. Here is an example!
<?php
$d = 1; //note that the numbers don't need to be wrapped with quotes
$w = 2; //another number
echo (10 - ($d + $w)) / (($w * $w) + ($d + $w)); //this is some basic maths, guessed the answer? The answer is 1.
echo "<br />"; //echoing html (for a line break)
$dw = $d += 4; echo $dw; //this will add 4 to $d and it will echo it. Note that we do not need to make separate lines for each bit of code, the semi-colon (";") tells PHP that a command is over
echo "<br />";
echo $dw -= 3; //it will subtract 3 from $dw (newly made string) and will also echo it
echo "<br />";
echo $w *= 4; //it will multiply $w with 4. Note - here we have used $w, not $dw
echo "<br />";
echo $w /= 2; //it will divide $w with 2
echo "<br />";
$d++; echo $d; //it will increase the value of $d by 1 and echo it
echo "
";
$d--; echo $d; //it will decrease the value of $d by 1 and echo it
?>
Here is an online demo. You can view the highlighted source here. That’s all with number! 😉
Hope you enjoyed the post and had loads of fun learning data types today! My next post will contain about the other types of data types which are floats, arrays, booleans and constants. We will also talk about typecasting.
Image Credits: IT Bench
8 Comments
Writing tutorials really consumes a lot of time.. so please appreciate 😛
I appreciate your effort Gautam. You are doing a great job.
Very nice explanation Gautam. I’m Waiting for next post of the series.
Hello three! I am really becoming addicted to this tutorials! Too bad I cant test all of em as I have exams nearby! Thanks to your TUTs than I can seriously start learning php after ma exams 🙂
Great Job.
Best step wise tutorial
@Mayur, Maharshi, Swashata, Harshil
Thanks 🙂
@Maharshi
The next posts will also come up soon
@Swashata
No problem, these tuts are always there now! (as they are published)
I know I’m being too much greedy…. But I would be waiting for some Javascript and AJAX tutorials too 🙂
Very nicely explained tutorial. Can you put tutorials on PHP5 explaining object oriented concepts?