Jump to content

Help Learning Php


tonymurray

Recommended Posts

I am just beginning with learning PHP and web development. I bought myself a copy of 'Beginning PHP & MySQL' to work my way through, but I've run into a problem which the book doesn't seem to answer and I can't find anywhere else.I'm guessing it's in the settings for PHP, but I'm not sure.When I output code in both html and PHP, the PHP bit shows up like it should but no html?EG.<html><head><title><?php echo "Welcome to my Web Site!";?></title></head><body><?php$date = "July 26, 2007";?><p>Today's date is <?=$date;?></p></body></html>When I open this in firefox all I get is 'Today's date is'. Nothing else shows up. (the above example is straight out of the book, so I assume that it's correct).Any help with this would be very much appreciated. Like I said, I'm very new to this, so it may be something extremely simple.Thanks,Tony

Link to comment
Share on other sites

PHP will only execute on a server that has PHP enabled, and only if it has a .php extension.
It's on a local host which was setup as the book instructed. The file was saved as a .php extension.Other examples have worked with php, but this one is not showing the html part.
Link to comment
Share on other sites

If you want to display text you need to use echo. Also <? did not work so i used <?php instead.

<html><head><title><?php echo "Welcome to my Web Site!";?></title></head><body><?php$date = "July 26, 2007";?><p>Today's date is <?php echo $date; ?></p></body></html>

Link to comment
Share on other sites

If you want to display text you need to use echo. Also <? did not work so i used <?php instead.
<html><head><title><?php echo "Welcome to my Web Site!";?></title></head><body><?php$date = "July 26, 2007";?><p>Today's date is <?php echo $date; ?></p></body></html>

Thanks,But it does exactly the same thing. No change at all on screen.I would be very surprised if the book was released with incorrect code, so I'm still thinking its in the settings or an error in the php.ini file ???
Link to comment
Share on other sites

The code from post 4 is not incorrect, it's dead-simple in that it sets a variable and prints it out, there's hardly anything there to even get wrong.Pull up the page in the browser and go to View->Source and check what's there. If you see exactly the same code, PHP tags and all, then the server is not executing the PHP code. If that's the case, these are the possible reasons:PHP is not installed or not workingThe file is not a PHP fileYou are not accessing the file through HTTPIf all of those are true, if PHP is installed and working, if the file is a PHP file, and if you are using HTTP to access it, then it will execute the PHP code. For future reference, it's better to use the long <?php ?> tags instead of the short tags, just because the short tags are disabled on some servers. Other than that, the code should run as posted in post 4.

Link to comment
Share on other sites

The code from post 4 is not incorrect, it's dead-simple in that it sets a variable and prints it out, there's hardly anything there to even get wrong.Pull up the page in the browser and go to View->Source and check what's there. If you see exactly the same code, PHP tags and all, then the server is not executing the PHP code. If that's the case, these are the possible reasons:PHP is not installed or not workingThe file is not a PHP fileYou are not accessing the file through HTTPIf all of those are true, if PHP is installed and working, if the file is a PHP file, and if you are using HTTP to access it, then it will execute the PHP code. For future reference, it's better to use the long <?php ?> tags instead of the short tags, just because the short tags are disabled on some servers. Other than that, the code should run as posted in post 4.
Here's the 'view source' as you suggested above<html><head><title> Welcome to my Web site! </title></head><body><p>Today's date is July 26, 2007 </p></body></html>It's now inserting the date as it should, but I'm still not getting the 'Welcome to my Web site!' displaying?I'm not exactly sure what you mean by 'accessing the file through HTTP'.I'm saving the notepad++ file as a .php file in apache2/htdocs. I'm then using the address http://localhost/'file name'.php. Does this help?Thanks.
Link to comment
Share on other sites

Using the http:// protocol is "accessing the files through PHP". The "Welcome to my web site!" text should appear in the title of the current window / tab, not in the actual body.

Link to comment
Share on other sites

Using the http:// protocol is "accessing the files through PHP". The "Welcome to my web site!" text should appear in the title of the current window / tab, not in the actual body.
Thanks Synook,between playing around with some of the settings in php.ini file and what you've just clarified for me here I finally made some sense of it and its doing what it should.Would've been nice if the book had explained it clearer and maybe shown what the outcome should have been.cheers,Tony
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...