We help IT Professionals succeed at work.

Beginner PHP

assaultkitty
assaultkitty asked
on
Medium Priority
605 Views
Last Modified: 2012-06-27
This is the output I got for my PHP scripts.  I am missing <? ?> output and the last output is displayed incorrectly.  Can anyone help me?  I am using Mozilla Firefox as the browser.

This text is displayed using standard PHP script delimiters.

This text is displayed using a PHP script section.

<% echo "This text is displayed using ASP-style script delimiters."; %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP Code Blocks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>
<?php
echo "This text is displayed using standard PHP script delimiters.";
?>
</p>
<p>
<script language="php">
echo "This text is displayed using a PHP script section.";
</script>
</p>
<p>
<?
echo "This text is displayed using short PHP script delimiters.";  
?>
</p>
<p>
<%
echo "This text is displayed using ASP-style script delimiters.";
%>
</p>
</body>
</html>
Comment
Watch Question

Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I'm not sure what your question is.  PHP needs to be run thru a web server that is setup to run the PHP interpreter.  Easy ways to do that are with WAMP http://www.wampserver.com/en/ and XAMPP http://www.apachefriends.org/en/xampp.html which install complete Apache/PHP/MySQL servers on a Windows computer.  Then you just need to run your scripts in one of the web server directories.  Some development packages will also do it.
RobOwner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
yes, what is your question?

You won't be able to have <% and <? tags in the same PHP document.  In other words you can't run ASP and PHP from the same document.
you need to have below setting set to ON in the php.ini
save, restart the web server

short_open_tag = On

; Allow ASP-style <% %> tags.
; http://php.net/asp-tags
asp_tags = On
RobOwner (Aidellio)
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
good point kelvin :)

Commented:
<script language="php">

Open in new window


That's not a valid option. PHP is a server-side scripting language as opposed to Javascript which is client-side. When you're using PHP on the server side, you would normally only use Javascript in a <script> tag. In fact you don't even need to specify the language anymore as browsers assume it's javascript.

Another thing to remember is that most web hosts don't enable short PHP tags. If you want your script to be easily portable between web hosts you should always use <?php. It's only 3 extra characters.
I'm not sure what you are trying to accomplish by using all these varied delimiters, but to be compatible under most systems the PHP delimiters <?php and ?> should be used.  You can use the others, but I've noted that they can cause problems.

Commented:
Also, <!DOCTYPE> needs to be the first line output to the browser. If it's not the first line the HTML standard says it should be ignored.

Your page should be simplified down to

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>PHP Code Blocks</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>
<?php
  echo "This text is displayed using standard PHP script delimiters.";
?>
</p>
</body>
</html>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
To quote the PHP online manual,...

There are four different pairs of opening and closing tags which can be used in PHP. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.

The correct PHP delimiters are <?php and ?>  The others are "edge cases" and should not be used because they are not supported in all PHP installations.  In particular, the short-open-tag that looks like <? creates confusion with XML strings.  As to the use of the ASP tags, please see these references:
http://php.net/manual/en/ini.core.php
http://php.net/manual/en/language.basic-syntax.phpmode.php

Notwithstanding the man pages, in practice the <script language="php"> tag is as rare as hen's teeth; I have never seen it used in a professional programming project.

Suggest you start your PHP adventures with a little reading.  Here are some great learning resources to help you get started.
http://php.net/tut.php
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray

Author

Commented:
I am sorry for the confusion.  I am doing a class exercise that has to have the four following scripts output  standard php script delimiters, the script element, short php script delimiters, and ASP style delimiters.  I am having problems with the last two (short php and ASP delimiters) as you can see.  I just wanted to make sure that what I am doing is correct and as stated before that the php may not be supported by my ISP (Comcast).  
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I second Ray's advice.  I only use <?php ?> in my own code.  I do have one customer whose site uses short tags <? ?> but I have never used the <script language="php"> </script> and ASP style tags.
Thanks for your reply.

As kelvinwkw so aptly pointed out, you will need to make changes to your php.ini file, howbeit I am not certain that even then you will get this to work.

Servers are used to handling one protocol at a time, you might have problems integrating the php shortcode <? and ?>, and the ASP tags.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
On my system the output looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP Code Blocks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<!-- STANDARD TAG -->
This text is displayed using standard PHP script delimiters.
<!-- SHORT TAG -->
This text is displayed using short PHP script delimiters.
<!-- ASP TAG -->
<%
echo $msg["asp"]; // NOTE CLOSING WITH ASP TAG
%>

<!-- SCRIPT TAG -->
This text is displayed using a PHP script section.

</body>
</html>
COULD NOT ENABLE ASP TAGS

Let me try to explain the code and the results.

First of all, we will need to look at the "view source" of the HTML to see the effect of the script, since PHP is a server-side language that generates HTML.  The HTML comments will not be apparent in the browser rendering.

Some things are "settable" in our PHP script and some things are not.  Since the variants of the open-PHP and close-PHP tags would need to be known to the PHP engine before it started parsing the script, the two optional tags (short and ASP) may or may not be settable.  We shall see whether/how this works as we run the script.

We want to see all the errors when we are writing and debugging code (lines 5-6).

We want to understand what is happening here, and what other PHP users have found, so we make a few searches of the PHP.net site and save the links to the man pages in the comments part of our script (lines 10-15).

We try to enable the variant tags, and we collect error messages if any part of the process fails (lines 23-36).  In the case of my system, the default PHP settings are in place (PHP 5.3+) and this means that short open tags are already on and ASP tags are off.

Next we create all of the data and HTML outputs that our script will produce (lines 40-67).  We do this before we start any browser output at all, even invisible whitespace.  This process is called "separation of logic and presentation" and it is one of the keys to good programming.  You will learn more about that as you learn more about PHP and program design.  Note the variable we set on line 40, and the way we use it on line 56.  The structure that starts on line 51 and ends on line 60 is called a "heredoc" block.  I find this very useful  for setting up a template that has PHP variables inside it.  You can learn more about heredoc notation on this page.
http://php.net/manual/en/language.types.string.php

With all our data prepared, we can now produce the HTML document.  We start that process on line 70 where we produce the top of the HTML document, then we close out our PHP script and drop into HTML to make the four tests for the different types of PHP tags.  This happens in lines 76-94.  As you can see, we have interspersed HTML comment lines in between the various test of the PHP tags.  This will give us a good visual signal about the success or failure of the process, and the messages that we echo inside each different test will also tell us about the process.

When all our tests have been rendered, we can terminate the HTML document (line 99).  

If we encountered any errors along the way, we want to see those, so we test to see if $err has anything more than the NULL we assigned on line 19.  If it does, we print it, and the program is complete.

As we look at the output of this script we can now see that ini_set() cannot help us adjust the PHP start and stop sequences.  It has to be done via the php.ini file.  Note the places that things are "changeable" on this man page:
http://php.net/manual/en/ini.core.php

Let me add a philosophical and stylistic comment about this script.  It is a demonstration script that shows, inter alia, that you cannot set the PHP tag style inside a PHP script.  To demonstrate that fact, we needed to use several start-and-stop sequences that intermixed the PHP code and the HTML strings.  Often, novice programmers think that the ability to intermix PHP and HTML is one of the coolest features of the language, so they write a lot of conditional structures like if() and while(), bouncing back and forth between PHP and HTML.  That sort of thing is barely tolerable if you're working alone on a very small project that will not be released into the wild.  But if you're building anything of importance, or working with other programmers, you want only one echo statement per script.  You can use heredoc notation and templates to render your HTML strings that contain the data you generated from your program logic.  As a rule, the fewer templates or heredocs you have, the better off you will be when it comes time to make a change or add a feature to your web site.  One template and one echo statement is a utopian ideal for the programmer.   In practice you will probably have several templates, each serving a different part of the client's experience, as different parts of the web pages are created.  But if you think about minimizing these things, it will help you develop code that is portable, reliable, extensible and ultimately valuable to you and your clients.

As you generate the internal data that will populate your web pages, you may want to see what the data looks like and what PHP is doing to the data.  Here is the most important function in all of PHP.  Now you know the secret; use it liberally!
http://php.net/manual/en/function.var-dump.php

Hope that helps explain it, and if you have any questions please post back and I will try to answer.  Best of luck in your PHP adventures, ~Ray
<?php // RAY_temp_assaultkitty.php


// WHEN TESTING, WE WANT TO SEE ALL THE ERROR MESSAGES
error_reporting(E_ALL);
ini_set('display_errors', TRUE);


// REQUIRED READING ON THESE MAN PAGES:
// http://php.net/manual/en/language.basic-syntax.phpmode.php
// http://php.net/manual/en/language.basic-syntax.phpmode.php#97113
// http://php.net/manual/en/ini.core.php#ini.short-open-tag
// http://php.net/manual/en/ini.core.php#ini.asp-tags
// http://php.net/manual/en/ini.php
// http://php.net/manual/en/ini.php#71462


// ANY ERRORS RE COLLECTED HERE
$err = NULL;


// TRY TO ENABLE ASP AND SHORT TAGS (MAY BE OFF IN SOME SYSTEMS)
if (!ini_get('asp_tags'))
{
    if (!ini_set('asp_tags', TRUE))
    {
        $err .= PHP_EOL . "COULD NOT ENABLE ASP TAGS";
    }
}
if (!ini_get('short_open_tag'))
{
    if (!ini_set('short_open_tag', TRUE))
    {
        $err .= PHP_EOL . "COULD NOT ENABLE SHORT TAGS";
    }
}


// CREATE THE PAGE TITLE
$pgt = 'PHP Code Blocks';


// CREATE AN ARRAY OF MESSAGES WITH ASSOCIATIVE (TEXT) KEYS
$msg["full"]   = "This text is displayed using standard PHP script delimiters.";
$msg["short"]  = "This text is displayed using short PHP script delimiters.";
$msg["script"] = "This text is displayed using a PHP script section.";
$msg["asp"]    = "This text is displayed using ASP-style script delimiters.";


// CREATE AN HTML INTRO BLOCK
$htm = <<<HTM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>$pgt</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
HTM;


// CREATE AN HTML FOOT BLOCK
$end = <<<END
</body>
</html>
END;


// START THE BROWSER OUTPUT
echo $htm;

// CLOSE OUT PHP AND START AGAIN
?>

<!-- STANDARD TAG -->
<?php
echo $msg["full"];
?>

<!-- SHORT TAG -->
<?
echo $msg["short"];
?>

<!-- ASP TAG -->
<%
echo $msg["asp"]; // NOTE CLOSING WITH ASP TAG
%>

<!-- SCRIPT TAG -->
<script language="php">
echo $msg["script"];
</script>


<?php
// END OF GENERATED HTML
echo $end;

// IF ANY ERRORS
if ($err) echo $err;

Open in new window

Ray, my gosh, you are a genius!  I just wanted to express my sincerest appreciation because you have given me an epiphany.

To give you some background, I have been a programmer since my teen years but it was only within the last 3 years that I began to apply my programming experience to web design.  As I am sure you are aware, once you know a set of programming languages, it all basically becomes the same when it comes to learning a new programming language.  You have to learn new syntax and maybe a few new protocols, but really at the core, the computer still processes everything instruction by instruction no matter what language you use.

So when I began my sojourn into the realm of web design, I applied my programming experience to PHP.  I never took any courses, just programmed "on-the-go".  Initially, this was no problem, because for the most part my first projects were relatively simple ones.

Now that I have been programming in PHP for a while, my projects are starting to become larger and more complicated.  I am spending more time addressing processing time and code efficiency issues.

Your little insight into the "One template, one echo statement" was like the sun dawning on a new day.  I wanted you to know that that one little comment will affect all my programming from now into the future.

Thank you!
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I support several websites that were coded as many little pieces of HTML, PHP, and javascript all mixed together.  When I am asked to make changes to these sites, it takes an hour to refresh my mind on how they were put together.  And when I am done with that one, I have to forget it to work on the next because the next is put together differently.  Finding errors can take a long time because of the piecemeal way the sites were built.  And major changes are next to impossible.  Starting from scratch would be easier in several cases.
HA!  DaveBaldwin, you have just described my life (and in particular, the life of a web designer).

This is why my rates triple when I go from creation to troubleshooting.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
@OmniUnlimited, thanks for your kind words.  And as far as creation vs troubleshooting, I think the quote from the famous firefighter Red Adair fits well: "If you think it's expensive to hire a professional, just wait till you hire an amateur!"
HA!  Again, right you are, Ray.  Right you are!

Thanks again.  You have been more help to me than you know.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.