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..
No comments:
Post a Comment