Pages

Wednesday, October 2, 2013

Exploring the PHP Control Structures Part 1

Written by: Lorenzo D. Alipio  

Remember in previous section "Introduction to PHP Syntax"?, I was able to list the following, but I did not discuss them any further. In this section, we will be exploring the most common control structures of PHP.

What is control structures really? What can they do to improve our program? Or, better yet what are their roles in writing PHP applications?

The easiest way to remember what control structures can do is that, they can control the application's response to any given events as they are defined in our application. This statement is true, because if we don't have any instructions on how the application is going to handle the user's response or events, our application cannot interact with our user. Let us write a simple code in PHP that will say something when the day of the week is Wednesday. Codes below demonstrate the if/else statements in PHP.


<?php

$today = date("l");

if($today === "Wednesday"){

echo "Today is ". $today ;

}

else{

echo "Today is not Wednesday";

}



?>

Codes above should output "Today is Wednesday", every Wednesdays of the week. Otherwise, it will output "Today is not Wednesday".

Let's add else if into the mix, and let make it echo "Today is Thursday" if $today is equal to Thursday.

<?php

$today = date("l");

if($today === "Wednesday"){

echo "Today is ". $today ;

}
else if($today ==="Thursday"){
echo "Today is ". $today;
}
else{

echo "Today is not Wednesday or Thursday";

}

?>


Noticed, how we set our rules in our simple application above? We set rules that only if Wednesday we echo the name day, and else if Thursday we also want to echo the name of the day. By using our simple example above, we can set many rules within our application.

Alternatively, we can use PHP switch statement instead of if/else/elseif statements. Below is a sample code equivalent to the code above.

<?php


$today = date("l");

switch ($today){

case 'Wednesday':

echo "Today is ". $today ;

break;

case 'Thursday':

echo "Today is ". $today ;

break;

default:
echo "Today is not Wednesday or Thursday";

}

?>

Another alternative is similar to a short-hand syntax. Please consider the codes below. It should return the same result as the above example codes.

<?php
$today = date("l");

echo (($today ==='Wednesday')? 'Today is'. $today : (($today ==='Thursday')? 'Today is'. $today : 'Today is not Wednesday or Thursday'));

?>

This conclude the tutorial Control Structures Part 1. Please come for the tutorial on loops on Control Structures Part 2.

Writing Your first PHP program

Written by: Lorenzo D. Alipio  
 
In previous section, we talked about PHP syntax. In this section, we will be writing our first PHP program. Let's do it shall we?

Hello World program
Step One: Open your notepad++ if it is currently open. Create an new php page called index.php, save this page in xampp/htdocs/tutorial.






Filename: xampp/htdocs/tutorial/index.php
 
<?php

/*
 * this is a comment block remember from previous section?
 */ 
 
 echo "Hello World";

// this is an inline comment
# this is an inline comment also
## and another inline comment

?>

Step Two: Make sure xampp is running. Open your favorite browser and direct it to http://localhost/tutorial/. If you are reading "Hello World" on your browser, congratulation! you just created you first PHP program.


 Using and Assigning string to PHP variables
!important! Always put a $ sign in-front of your variables.



 




<?php

$saySomething = "Hello World";
$sayOtherthing = 'Hello There';

echo $saySomething;

echo "<br/>";  

echo $sayOtherthing;


?>

our second code example above should display the text strings output "Hello World" and "Hello There".


Single Quotes or Double Quotes?

Good question, if you noticed our variable $sayOtherthing was assigned an string value wrapped with a single quotes. The use of single quotes and double quotes is for the purpose of escaping a string.

for example if we are to properly escape this piece of code, <a href="google.com">Go to Google</a>. If we will be echoing the link in PHP, we need to escape the quotes in href. So, our codes will look like this

<?php

echo " <a href=\"google.com\">Go to Google </a> ";

?>

The codes above is perfectly normal and it is acceptable. An alternative way of writing the above codes is to use single quotes. By using single quotes, we can eliminate the use of slashes, for the purpose of escaping qoutes.

<?php

echo ' <a href="google.com">Go to Google </a> ';

?>

This concludes the "writing your first PHP program section". In the next section, I will be covering some of the php Control Structures. That's it for now.. and please keep on practicing what you have learned today..


Introduction to PHP syntax

Written by: Lorenzo D. Alipio  

What is PHP Syntax? 
PHP Syntax is the format and it contains the set of rules on how PHP program should be written so that it can be interpreted by PHP parser in the web server. Together with the Syntax is another term called Semantics. Semantics is the meanings of symbols, text, characters that can be use in writing PHP program.

What are delimiters in PHP?
Delimiters are symbols that allows the parser to know where the PHP codes begins and where it ends. It is like a set of instructions on how are we going to stack a boxes with a bold printed instruction "This Side UP", or the sign in our local Department of Motor Vehicle  "Line Start Here".

PHP have a commonly use delimiters.
1. The standard tags

      <?php     ?>

between the <?php  and ?>, we can write our PHP variables, functions, classes, and other PHP codes that make up our program.

2. The short hand tags. ( If you are a beginner, I strongly suggest to refrain from using this, until you become familiar with the language. There is nothing wrong using the short hand, but I prefer not using them. It is all a matter of preference).

      <?   ?>

The same as long form, we can write our codes between the opening and the end delimiters.

3. The script tags .

      <script language="php"></script> 

4. ASP like tags. This has to be enabled in the php.ini file. We will be talking about php.ini file at a later date.

    <%   %>

Below are examples of the above tags, please take a look at the codes below and they are all have the same output "Hello World". Assuming that all necessary settings in the php.ini has been enabled.

Standard Tags:

<?php

       echo "Hello World";
?>


Short-hand Tags:

 <?= "Hello World" ?>


Script tags:

<script language="php"> echo "Hello World"; </script>


ASP like tags:

<% echo "Hello World"; %> 


Throughout this tutorial, we will be using the standard tags, because for the reasons I have already mentioned above.

What are the basic PHP language constructs?

1. echo = to print string, value of variables, an output of a function or method. Alternatively, we can use print. Many coders of PHP are debating which one is better echo or print. This is beyond the scope of this tutorial. Yes, these 2 have some minor differences, but it is something we need to be worrying about for now.

2. $ = All variables in PHP must have a dollar sign in-front of them. For example $myVariable

3. Case sensitivity = PHP is  case sensitive. So, for example the following variables are not the same and will be treated by the parsers as three different entities.

$myvariable , $myVariable, $myVariaBle 

4. Assignment operators

  •     =   this is use to make something equal to something. For example, $myVariable = 'Hello World';
  •  => value and key operator . This is use mainly in the array. For example, $myArray = array('name' => 'John', 'last'=>'Doe');

5. Control Structures .

6. Comment tags. In PHP, we have three commonly use tags for commenting within our application.
  • # and //  both tags can be use for inline commenting. e.g.  // this is an inline comment  and # this is an inline comment .
  • /* and */ these tags are use in comment block. Example of usage.. /* this can be a multiple lines of comments or block */ .

This ends the introduction to PHP syntax tutorial. On the next section, we will be writing our first PHP program. Please stand by.

Introduction to PHP: Introduction

 Written by: Lorenzo D. Alipio  (php back-end developer)


What is PHP?
According to PHP.net,
PHP is a popular general-purpose scripting language that is especially suited to web development.
Fast, flexible and pragmatic, PHP powers everything from your blog to the largest social networking site in the world.
PHP is also known as Hypertext Preprocessor. In this series, we will focusing on the basic constructs of  PHP. The second  series will be concentrating on arrays, third series will on loops, and fourth will be focusing on writing our own reusable functions.

Why learn how to program in PHP?
 There are more than 200 million websites written in PHP today. There will be a high demand for talented programmers now and in the future. Otherwise, you can just use your PHP knowledge in creating your own dynamic website.

Is PHP programming for me?
The answer to this question is all up to you. Some people fell in love with PHP since day one, while others ran and take on another programming language.

Is PHP the only language use for creating websites?
No, PHP is not the only language use for creating websites. In fact, we can build a website without even using PHP. However, if our website have dynamic contents, we can use PHP, Python, Ruby, or ASP.net.

Which web development language is better?
The answer to this question is rather argumentative in nature, but I always believe that programmer should write their web application in the language they are the most comfortable with.

Do I need to know HTML?
Not, necessarily, but it will be in extremely useful if you already know HTML, jquery, javascript.




Is there any requirements?
Yes, and here are the requirements.

1. XAMPP or equivalent Apache MySQL PHP servers installed on your computer. For alternative, there is an Apache MySQL PHP development environment called WAMP stack from  BITNAMI. I personally prefer XAMPP, because of the integrated Tomcat Apache for java applets development. If installing XAMPP, make sure it is installed on C drive and not in the program directory.
 
3. A suitable PHP source code editor. I am currently using notepad++. Install this as you would normally install windows application.

 
4. Optional (any) : NetBeans IDE, Eclipse, Aptana Studio 3.


Install the above recommended requirements on your desktop. After successful installation, run the xampp control panel. Click on start for the apache and mysql. MysQL is not needed in this tutorial, but is nice to have it running for you to discover. XAMPP is running if your control panel appeared to be like the screenshot below.


I am assuming here that XAMPP is installed in C: drive, if not just change it to your letter drive as it applies to your xampp installation location.

Open the xampp/htdocs/ directory and create a new directory called "tutorial". This is the directory where all of our practice codes are to be saved.


    xampp
          htdocs
                    tutorial

 


Click on the notepad++ desktop icon and make sure it is running correctly and it is ready for the next tutorial.