#1: PHP Tutorials for Beginners – Introduction

This entry is part 1 of 7 in the series PHP Tutorials

This is post post in new series started by PHP Tutorials for Beginners by Gautam. This post provides general introduction to PHP and some historical facts about it! As PHP if language top-blogging platform Wordpress uses, it is good to have little understanding of PHP. This will help you for sure in your blogging carrier even if programming is not your prime occupation.  (Added by Editor) Read the rest of this entry »

10 comments Add Your Comment Read More

#2: PHP Tutorials for Beginners – Recommended Programs

This entry is part 2 of 7 in the series PHP Tutorials

Download ButtonHi again. Well, before starting off, I want you to download some softwares which will help you a lot in coding. These are recommended programs, and are not necessary. But in the tutorials, only these softwares will be used, so I request you to download & install these. As I use Windows Vista, I will be giving the tutorial to install the programs for Windows. Links to download & documentation of same programs for other OS also given in this post. Read the rest of this entry »

9 comments Add Your Comment Read More

#7: PHP Tutorials for Beginners – Loops

This entry is part 2 of 7 in the series PHP Tutorials

CodingIn the last post, we learnt about the first kind of control structures which are, if statements which allowed us to run a block of code if a condition is true or not. But what about writing a code which would run a block of code again and again? That’s where we will need loops for. Loops allow us to execute a block of code again and again if a condition is true.

In this post, we will talk about different kinds of loops like – While Loop, Do-while Loop, For Loop and For-each Loop. We will also talk about – Continue, Break and Pointers. Read the rest of this entry »

no comments Add Your Comment Read More

#3: PHP Tutorials For Beginners – Writing The Code

This entry is part 3 of 7 in the series PHP Tutorials

CodingWelcome Back! Today we will start with the basics on – “Writing the Code”. We will talk about how the PHP code is written, simple basic code like echoing Hello World, adding comments in the PHP file etc. We will also talk about the official site of PHP and how it can help you discover new things. I hope that you have installed the recommended programs of which I had written a couple of days back.

Read the rest of this entry »

13 comments Add Your Comment Read More

#4: PHP Tutorials for Beginners – Data Types

This entry is part 4 of 7 in the series PHP Tutorials

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

7 comments Add Your Comment Read More

#5: PHP Tutorials for Beginners – Data Types (Continued)

This entry is part 5 of 7 in the series PHP Tutorials

Diving into PHPIn the previous post, we talked about some data types – variables, strings, string functions and numbers. In this post we will talk about more data types which are floats, arrays, array functions, booleans and constants. We will also talk about type casting. So, without wasting time, let us start! Read the rest of this entry »

4 comments Add Your Comment Read More

#6: PHP Tutorials for Beginners – Logical Expressions

This entry is part 6 of 7 in the series PHP Tutorials

Coding ImageAs we have already learnt about data types, we will now study about logical expressions and also about switch. Logical expressions are basically if else statements. You might have given orders to somebody like if jam is available then apply that on bread else if butter is available, then apply that else don’t apply anything. We can also write such commands in PHP. It is a very important and useful topic. So let us learn about it! Read the rest of this entry »

3 comments Add Your Comment Read More