Jump to content

PHP 101


msf004

Recommended Posts

Hello,

 

I am a developer of 20+ years with C, Shell Scripts, CGI, Java, JSP, ASP, etc. just not PHP. 

 

I recently had a need to start using PHP and I quickly ran into a problem to which I have not found an answer through searches.  It seems like a strange problem, or maybe just my misunderstanding of the language.  Either way, I am interested in finding an answer.  Here is the problem.

 

I have three files as shown below:

 

FileA.php

-----------

<html>

<head></head>

<body>

<?php

  echo "hello world";

?>

</body>

</html>

 

 

FileB.php

-----------

<html>

<head></head>

<body>

<?= "hello world" ?>

 

 

FileC.php

-----------

<?php

  echo "hello world";

?>

 

 

I put these files into the same directory under Apache (htttpd).  Then from an Internet browser I browse to the files, File C shows the expected output of "Hello World" whereas File A & B show a blank page.  Further, if I view the source of File A or B from the Internet browser, the source shows the PHP code; exactly as shown above.  I.e.: the SOURCE for File A when viewed from a web browser is:

 

<html>

<head></head>

<body>

<?php

  echo "hello world";

?>

</body>

</html>

 

Basically, using the formats above, Files A & B, seemingly are not being processed by the PHP Interpreter at all; however, the text is also not going to the browser - it just shows up in the source of the HTML.

 

So, my question is, Why?

 

I am running PHP 4.3.2-40 on Red Hat ES3 with Apache HTTPD 2.0.46.  I have browser tested with Mac OS X Safari, MAC OS X Camino, Windows FireFox, Windows IE.

 

Any thoughts, suggestions, or assistance would be greatly appreciated.

 

Thanks,

-Marc

 

 

 

Link to comment
Share on other sites

<?="hello world"?> ... will fail unless short tags are enable in php. Avoid this construction.

 

As to why you get the results you see, php is a server-interpreted scripting language, i.e. it only works when parsed on a server then rendered by a browser (if that's what you're trying to do). Navigating locally to a file - as it appears you are trying - will fail.  You need to navigate to http://localhost ..... your file to ensure it is delivered by the server.

Link to comment
Share on other sites

If C works as expected and A shows nothing (except the script source when you view source), I'd expect that A is not absolutely the same as you posted here. Perhaps it's < ?php and not <?php ??

Link to comment
Share on other sites

<?="hello world"?> ... will fail unless short tags are enable in php. Avoid this construction.

 

As to why you get the results you see, php is a server-interpreted scripting language, i.e. it only works when parsed on a server then rendered by a browser (if that's what you're trying to do). Navigating locally to a file - as it appears you are trying - will fail.  You need to navigate to http://localhost ..... your file to ensure it is delivered by the server.

 

I did not literally browse to the files on my local file system.  I placed the files into a configured Apache httpd directory and utilized a web browser via: http://<servername>/<directory>/<filename> to have the PHP interpreter parse the files and send a response.

 

Thanks,

-Marc

 

Link to comment
Share on other sites

If C works as expected and A shows nothing (except the script source when you view source), I'd expect that A is not absolutely the same as you posted here. Perhaps it's < ?php and not <?php ??

 

 

No, I have literally copied and pasted the files.  That is why I am so confused.  I even went back just now to *ensure* there is no spacing, I.e.: <?php and not < ?php or <? php

 

The file is exactly as shown - this is why I am not understanding the issue.  I assume php works just like ASP and JSP - and I have used both of those languages for 10+ years.  I was thinking maybe it is a configuration thing somewhere.

 

Ultimately, I have tried about 10 tests and consistently, for all of my tests, if <?php is not the FIRST line of the file then the PHP Interpreter is not parsing the file.

 

Thanks for your responses.

-Marc

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.