Difference between revisions of "/Hello World/"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Hello World '''Author: Anurag Jain, Abhishek Kumar Srivastava''' '''Keywords: localhost, client-server''' {| style="border-spacing:0;" | style="border-…')
 
(No difference)

Latest revision as of 15:18, 26 December 2012

Title of script: Hello World

Author: Anurag Jain, Abhishek Kumar Srivastava

Keywords: localhost, client-server


Visual cue
Narration
Show Slide 1
Hello, Welcome to the spoken tutorial for PHP and MySQL. In this tutorial we'll show you how to write basic PHP program and execute over Apache server. Moreover you'll learn how to embed PHP script in HTML, how to print a string and concatenate two different strings in PHP. So basically with this tutorial, we will work, for the first time, on PHP platform and give a start-up to our programming.
Show Slide 2
As we'll be working on windows platform, before we start writing our codes, we need to check certain things.

1: We'll be using XAMPP version 1.7.3 or later which have PHP 5.3.1, MySQL 5.1 and Apache 2.2. It is advised to use latest version of XAMPP only.

2: Click XAMPP icon from your task bar or start menu.

3: Open XAMPP control panel running in your task bar and start Apache service.

4: Open notepad++(text editor). We'll use it as our editor throughout the tutorial ;

Show Slide 3
Basically PHP codes are embedded between HTML tags as per the need. It provides the flexibility and freedom to the user to display dynamic content in an organized manner.So let's start by writing a basic and conventional “Hello world” program which will print the string “Hello World”.
Switch to editor and
type the code
 
<html>
   <head>
     <title>Hello World</title>
   </head>

 <body>
   
   <?php  //opening PHP
   echo "Hello World"; //prints string
   //closing PHP
   ?> 

 </body>

</html>
Show Slide 4
Let's have a look at the code.
  • Its quite self explanatory, we use "<?php" to denote the start of PHP script and ?> for closing it. So this way we can start and close PHP script wherever we want, in between the HTML tags.
  • 'echo' is the function used to print a given string. We can also use single quotes(') around the string instead of double quotes. This is because PHP treats both as same string. Comments in PHP are given in same manner as in C++. We'll be using // for single line comment and /* */ for commenting multiple lines.


Show Slide 5
Now before we run this code please note that you need to do following things.
  • Now saves the file in the 'htdocs' folder of XAMPP. By default on windows platform the path is 'c:\xampp\htdocs\' as hello.php.
  • Now open your web-browser like Firefox or Chrome and type http://localhost/hello.php or http://127.0.0.1/hello.php and you will see the printed Hello world string.
  • Here is one concept which I'd like to make clear. Basically, the communication in a client server model is done via a network address, the IP address and a communication interface called Port.Since the Apache service is running on your local machine on port 80, your machine it self will act both as a client and a server. Request will be sent through your browser which requests hello.php page from server. This request is processed by Apache by compiling hello.php file stored in its htdocs and return the HTML to the browser.
  • This HTML generated can be seen by viewing the source of the page in your browser. You can see no PHP code is displayed to client side, as it gets processed on server side and thus remains hidden and just the plain HTML is fetched.

Now lets make some modifications in our previous code. You can embed HTML tags into PHP to define the properties of your contents on serverside.

Switch to editor and
type the code
 
<html>

   <head>
     <title>Hello World</title>
   </head>

   <body>

   <?php 
     echo "<b>"."Hello"." World"."</b><br>";/* <b> is a HTML tag for printing text in BOLD */
                                            /* '.' is a PHP operator for joining (concatenating) two strings.*/
                                            /* <br> is a HTML tag for changing it to new line.*/
     printf ("hi"); //a 'c' type function to print strings.
   ?>

   </body>

 </html>
Show Slide 5
Run this code in similar manner after saving it and you will see some difference.
  • The Hello World string becomes bold by using <b>tag.
  • And a linefeed is given printing 'hi' in next line.
  • Also 'printf()' is another function similar to 'echo' which prints the given string.
  • The '.' is a PHP operator to join(concatenate) strings together.

This way we can generate HTML using PHP code. But this is not a good practice to generate unnecessary HTML code using PHP.

Show Slide 6
Well I think by this tutorial you would have learned to write and execute basic PHP programs and execute them over Apache on the local machine. You'll be learning more language grammar and syntax in the upcoming tutorials. Thank you.
Show Slide 7
Spoken Tutorials are a part of the Talk to a Teacher project,coordinated by www.spoken-tutorial.org, supported by the National Mission on Education through ICT, MHRD, Government of India. More information on the same is available at the following link http://spoken-tutorial.org/NMEICT-Intro. The script for this tutorial has been contributed by Abhishek Srivastav from SASTRA University and myself. This is Anurag Jain from SASTRA University Thanjavur, signing off. Thank you for watching.

Contributors and Content Editors

Gyan