PHP print text

All php code starts with the following code <?php and always ends with ?>

While you can use shorter versions, for example: <? ... ?> it’s more consistent and safer to use the full php tags.

As you’ll have noticed with our first page using phpinfo(), the line of code is terminated with a semi colon. These are used to terminate every line of code besides control structures (more on these later). PHP and HTML code can be mixed in one page, however they cannot cross each other, ie. all php must be enclosed with the php tags, and if HTML is to be used within PHP then it must be presented in a particular way (see later).

So a simple page to print the typical ‘Hello World’ statement <html>
<head>
<title>Hello World Practice</title>
</head>
<body>
<?php print "Hello World"; ?>
</body>
</html>
Another word which does the same as “print” is “echo”, however this tends to cause some syntax problems at times and seeing as there is no difference I always use print.

When using print, all strings are enclosed with double quotes. Single quotes can be used but a more consistent approach is double quotes. If you want to display a double quote within a print statement then you just escape it with a backslash eg. <?php print "He said \"Where are you going?\", and then walked away"; ?>

When this is interpreted, the backslash tells the interpreter to treat the double quote as a string and not a double quote. The following would not work: <?php print "He said "Where are you going?", and then walked away"; ?>

This would give a syntax error. However single quotes within double quotes are allowed in this situation so: <?php print "He said 'Where are you going?', and then walked away"; ?> would work fine.

You may also like...

2 Responses

  1. ryan says:

    Sarah,
    great info here!

    one question on a php blog script i added to a clients site…

    how do i add an additional link, next to these 2 links ??? (i need a link to go back to the original websites home page)

    ———————
    $nav_menu =”“.$lang[‘Home’].” | “.$lang[‘Admin’].”“;
    ———————

    help appreciated. – thanks.
    ryan lund
    rdldigital.com

  2. Sarah says:

    Hi Ryan,

    For your code above you can add the link as a standard string i.e.

    $nav_menu = $lang['Home']." | ".$lang['Admin']." | <a href='/index.php'>Home</a>";

    Hope that fixes your problem, if not you’ll have to let me know how you want it to appear or where you want the link to appear and I can alter the PHP code above.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: