Getting Started with PHP: Your First Step into Web Development
So let’s start writing PHP code, wait before you can run PHP code successfully you have to install PHP on your computer, you also need a local server to run the code straight away on your computer. In our case, we will be using Apache server and a database to store data, we will be using MySQL to handle this part. To configure all these can be very tedious that’s where another application called XAMPP or WAMP or LAMP comes in. WAMP means Windows Apache Mysql PHP, LAMP means Linux Apache Mysql PHP while XAMPP means X(either mac or windows or Linux) Apache Mysql PHP PERL… These packages save you from the stress of configuring the environment to run your PHP codes. So download the one you like and let’s start.
If you downloaded XAMPP or WAMP, go to user C drive or your root drive and find where the application was installed, if you downloaded XAMPP look for ‘htdocs’, and create a folder within it, name it ‘myfirstpro’ or anything you like, if yours is WAMP then look for ‘www’ folders, navigate inside and create a new folder. Inside this folder create a file and name it hello.php, Note that the file extension is ‘php’ now open your code editor and let’s code.
Remember to turn on your WAMP or XAMPP or MAMP.
<?php
echo “Hello from the other side”;
?>
Now open your browser and type:
localhost/myfirstpro/hello.php
You should see ‘hello from the other side’ displayed on your browser
Now that everything seems to be set, let’s proceed;
<?php $varible = “hello from the other side”;$variable1 = “I must have called a thousand times”;echo $variableecho $variable1echo “$variable $variabel1”;echo $variabel.’ ‘ . $variable;?>
Observe the result of each print statement, Note that ‘echo’ is used for displaying something to the screen; $ is placed in front of the variable name so that the PHP interpreter can recognize it as a variable name, what is a variable? variables are used to store information to be referenced and used by programs for example, in maths you can have x=4, which means x is 4, x is the variable name, 4 is the value. The same principle applies here as well. Hope are you getting the logic?.
The next step is to understand data type in PHP, Data type is a particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it. For example:
<?php
$x = 5 //integer: you can see its a number
$y = “hello world” //String: you can its a word
$y = true // Boolean: This is can represent two state, true or false
?>
To learn more please check this out.
Our next step is to learn some string functions in PHP. String functions are built in function that comes along with php. For example.
<?php $x = dog echo strlen($x);//this print the number of number of letters in the variable echo ucwords($X);//this converts the first character of each word in a string to uppercase echo ucfirsts($x);//Converts the first character of a string to uppercase ?>
please check out the complete lists on function available here.
Now that you have understood that, let’s do something a bit exciting
<?php $jack = dog; if($jack==Dog){ echo “jack is a dog”; } else{ echo “jack is a man” } ?>
In the code above we declare variable jack as dog, we then check to see if Jack is a dog or not. This is called conditional statement, Conditional statements are used for checking if something is true or false, conditional statement makes use of if-else statement to perform its operation. Notice is operator ‘==’ this is called conditional operator.
‘==’ means equals; ‘<’ means less than; ‘>’ means greater than; ‘< =’ means lesser than or equal; ‘> = ’ means greater than or equal; ‘!=’ means not equal to
Now let’s assume you wanted to count 1 to 100 numbers, instead of echoing out 1,2, 3, … , 100 you can just use loop statement to do the counting easily. In short loop statements are used when doing something that involves repetition.
In PHP there are 4 types of loop statement,
<?php $x = 1; while($x<=100){ echo “$x <br>”; $x=$x+1; } ?>
What the code above does is very simple, we set x as 1, we then iterate over a count of 100 that we are said that while x is less or 100, as long as x is less or equals to 100 print x, $x=$x+1, means after printing x equals 1, add 1 to it again, which now make it 2, check if x less than or equals 100, if not then add 1 to it again, now x is 3, it keep adding 1 to it and it keep checking if x is less or equals to 100 until the condition is no longer true, that is, x has reached 100, so it stop counting . That is the basis of loop statements.
Another concept I will write about in this tutorial is the Arrays.
An array stores multiple values in one single variable.
<?php $sittingRoom = array(“fans”, “tables”, “chairs”); echo = $sittingRoom[0] . ‘<br>’; //print fans echo = $sittingRoom[1] . ‘<br>’; //print tables echo = $sittingRoom[2] . ‘<br>’; //print chairs ?>
We defined a variable called sittingRoom and echo the content out one after the other, note that we started from [0]. In an array, the first element is 0, followed by 1, and so on. To access the value in an array you’ll write the variable name that defines the array and add the number of the value you wanted to access belong to, you put the number in a square bracket [], eg $sittingRoom[2].
Inserting HTML tags within php tags
1 <?php 2 echo ‘<p style=”color: red”> Hello from the other side </p>’; 3 $x = “hello”; 4 echo ‘<p style=”color: blue”> ‘.$x. ‘ from the other side</p>’; 5 ?>
On line 3 we assigned ‘hello’ into variable x, and on line 4 we inserted the variable x within the p tag notice that period sign(.) This is called concatenation.
In Conclusion
If you understood all that I have been discussing so far, I think you should be ready for the next step, which is connecting to a database and building an application that performs the CRUD (Create Read Update Delete).
EPISODE #2 coming soon
Leave a Reply