This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Your first page - Hello World!


Now that you know the PHP syntax, let's look at actually using it.
Before we go on, it's important to know that PHP can be used in any part of any page. Before any HTML is processed, after your </body> tag, and everything in between.

Let's make our first page; Hello World.

<html>
<body>
<?
    # This will display your text
    echo "Hello World!";
?>
</body>
</html>

Looks like:

Hello World!


Now to break this down:

  • Open HTML and BODY tags
  • Open syntax - allows us to use PHP
  • When you call on the "#", it creates a comment. Comments are not useful for coding, but they are great for leaving notes for yourself or others, and debugging.
  • "echo" is the command (referred to as a function), and will display the text between the quotes. Open quotation, text, close quotation, semicolin.
    The semicolin means that's the end of that statement - finished and PHP will look for the next command to execute.
  • Close syntax
  • Close HTML and BODY tags

Comments:
There are 3 types of comments, but they all do the exact same thing.

  • # - an entire line for a comment
  • // - last part of the line comment
  • /* comment */ - block comment

Examples of these look like this:

<?
		# this will cover the entire line
		echo "Test"; // this is typically used after something important
		/* and this will
		cover everything
		in this section */
	?>
    

Semicolins:
After your line of code, or statement (Except for comments), always use a semicolin. NEVER forget them!

echo function:
the echo function is the same as print in many other languages. I recommend using echo because it's one character shorter. 20% faster to load, 20% less typing.
Anytime you want to display some sort of text, you use the echo function. Your text can be enclosed by double quotations or single. If it started with double, for example, it has to end with double.
example:

<?

echo "1. this will show up <br />"; // echo also accepts html
echo '2. this will show up <br />'; // single quotes work
echo "3. this will NOT work <br />'; /* starting with a double quote, ending with a single is erronous */
echo '4. this won't work either, can you figure out why?';

?>

The first two lines work flawlessly.
The last two will not work. Line #3 starts with a " but ends with a ' - which will cause a "Parse error:"

Parse error: syntax error, unexpected $end in /home/client/public_html/directory/echo.php on line 6

Exercise:

Figure out why echo #4 gives you a parse error.
Hint: look at the colors in the example

Your task:
Create a new php file called echo.php and get it to display some text. Upload it to your server, and message me the link. If you have questions or need help, feel free to message me or consult the internet guru of all, Google. 

Task Discussion


  • 0be1   Aug. 21, 2013, 3:07 p.m.

    Here is my echo assignment

    http://sdgwebdesigns.com/p2pu/php/echo.php

    The example #4 gives an error because of the ' in the word won't. In order to properly list that word, it would have to be type as follows:

    won\'t

    That will allow the line to properly be parsed and give the desired output.

    SIncerely,

    Shawn

  • Christina   July 1, 2013, 11:07 a.m.

    Here is the uploaded echo.php file to see.

    http://anthrogeek.com/sandbox/echo.php

  • Daniel S   April 9, 2013, 1:53 a.m.

    'Hello World' all done.  (Hoping you can open it from my box account....)

  • Tynanh   March 15, 2013, 8:16 p.m.

    My first php

    echo.php

  • Anonym   Feb. 15, 2013, 11:57 p.m.

    Here is my page: http://practicephp.net63.net/echo.php

  • Richard   Dec. 18, 2012, 5:11 p.m.

    <?php

    echo "Spelling: NOT semicolin BUT semicolon";

    ?>

  • MF   Sept. 9, 2012, 2:45 p.m.

    http://www.marianaphp.tit4tat.co.uk/echo.php

  • Jay   Sept. 6, 2012, 6:55 p.m.

    Hi. here is my first hello world with php ^_^ http://jayextremes.com/sandbox/echo.php

  • Manuel   Aug. 19, 2012, 11:26 p.m.

    Here is the link to my first php page. - http://roldanvega.host-ed.me/

  • Sakejazz   Aug. 15, 2012, 2:13 a.m.

    here is my first hello world page. http://tolong.comlu.com/echo.php

  • Alejandro   April 23, 2012, 6:23 p.m.

    Hi, Folks. 

    This is the link to my firts php: www.sitiosguau.com/echo.php

     

    It's always necesary use the <html> and <body> tags or I can just begin the file with the <? tag?

     

    Cheers and thanks for this awesome course smiley

  • xzorzist   April 14, 2012, 6:33 p.m.

    Code for task:

    Results

  • Anthony Tolbert   Feb. 21, 2012, 12:43 a.m.

     

     

    Here is my first php page

    http://www.anthonysdesign.com/test/testphp.php

  • W8RDG   Feb. 9, 2012, 2:07 p.m.

    Here's my echo page.

  • Javier   Feb. 7, 2012, 3:19 p.m.

    Here is my first page

  • Luis Sierra   Jan. 25, 2012, 3:43 p.m.

    This is my first php page

    Let's continue learning!!

  • Lavinia   Dec. 13, 2011, 1:08 p.m.

    hello world! :)

     

    this is the link to my first php page, I'm so excited to learn this

    http://etat-dame.com/echo.php

     

    Thanks

    Lavinia

  • Yvve   Nov. 20, 2011, 6:31 a.m.

    Here is the link for my first page in php. http://www.yvonnej.altervista.org/php5/p2/echo.php