Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Beginner - Trying to understand why echo is not printing correctly?

printing

  • Please log in to reply
3 replies to this topic

#1 Zukky

Zukky

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 06 September 2011 - 06:17 PM

Hi people! I'm new to this forum, so a very warm welcome from me :).

I have just started PHP because I want to construct a website and i'm trying to figure out what is going wrong?

HelloWorld.PHP - basic program trying to make it work. I'm using Adobe CS5 Dreamweaver.

Source code:

<html>
<body>
<?php
echo 'Hello';
?>
</body>
</html>

This prints nothing. I checked on Google Chrome, Firefox and IE. It doesn't print anything. Why?

I used DreamWeaver PHP template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo 'Hello';
?>
</body>
</html>

Still printed nothing. Then I made this change:

<html>
<body>

<?php
echo '<p>Hello</p>';
?>
</body>
</html>

This outputted:

Hello
'; ?>

Why does it output " '; ?> " when that is not in the parameters? I can't seem to get rid of it

Do I need XAMPP? Is that the problem? Because all I am doing is making it on dreamweaver with .php extension and dragging it onto Chrome and/or Firefox to see the result.

Thank you for all your generous and helpful replies :),

Zukky.
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 06 September 2011 - 07:43 PM

There are two possibilities I can determine right away,
  • Does the file you are running in the web browser have a .php extension?
  • Are you viewing the php file directly (i.e. c:\...\file.php) ?
You will need a local web server to parse the PHP page, as it is ran on the server side, to generate a client side result (in this case its "hello" paragraph.)

If you browse the source of the empty page on Firefox, it may actually show you the PHP code hidden in pink as the browser is not really supposed to be viewing PHP, only its result.

If this is the case, feel free to look at the FAQ of this forum section, there are links to XAMPP to (easily) get a basic server capable of PHP parsing on your computer, so you can view http://localhost/page.php to test just as you would on a web server.

If I have mistaken, just tell me.
Alexander.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Zukky

Zukky

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 07 September 2011 - 02:12 PM

Hi Alexander,

Thank you very much for your advice. I didn't really understand that it was server-sided, now I do :). Thanks for that ;).

It does have a .PHP extension, yes I am viewing directly.

I installed XAMPP and everything seems to be working fantastic :)! So when I upload this to my website FTP, it should parse and work correctly? (Stupid question i'll test and report back). I have a domain name registered with GoDaddy.com and their hosting servers too. If you have any experience with them, do you have any good tips with FTP and PHP use?

I want to add a Google Maps API to my site. Would it be smart to have HTML with a query box that is displayed on the PHP page on a Google Maps API?

Thank you,

Zukky.
  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 07 September 2011 - 05:17 PM

So when I upload this to my website FTP, it should parse and work correctly?

XAMPP is essentially a slimmed down package containing most of what web hosts usually use (Apache, with PHP as a module or similar and MySQL for database testing) so uploading it to your site will work nearly the same as running it on your local XAMPP.

The major difference will be settings however, web hosts often restrict settings and may apply PHP's safe mode which will disallow things such as fopen to be used on web addresses. Although most of safe mode's restrictions only affect specialty applications, you may not easily be able to retrieve data from an URL with PHP, which may or may not be a problem for API access.

If you have any experience with them, do you have any good tips with FTP and PHP use?

FTP is an older insecure protocol (you can capture ftp login passwords in plaintext, if somebody were able to be on your network), however it is simple to implement and is what most shared web hosts provide. Generally it is not an issue as long as you've no malware and a strongly passworded router or wireless network.

PHP wise you should always develop offline on XAMPP, trying to match settings if applicable to your web host's. Be sure to test every aspect of your application, this can help avoid putting untested code online, and have somebody use some automated script testing various exploits or malformed input (i.e. user names) to try to exploit the database.

An at least brief understanding of what you can do to secure PHP applications will be golden, such as the rule "never trust user input", always make sure that it is properly handled and cleaned, so that they cannot type anything they shouldn't.

I want to add a Google Maps API to my site. Would it be smart to have HTML with a query box that is displayed on the PHP page on a Google Maps API?


You can embed PHP within HTML (as long as it is a .php extension). For example, assume the HTML form is designed to POST (method="post") to the same page, you can use a PHP script to handle its contents (or lack of contents, such as when they first view the page)

A rough example:

<form action="thispage.php" method="post">
<input type="input" name="form_location"/>
<input type="submit"/>
</form>

<?php
if( isset($_POST['form_location']) ) {
//query the map API for the data in $_POST['form_location']
} else {
echo "No location has been entered";
}
?>



There are many ways of doing this however, and Google may allow AJAX instead of PHP.

You may wish to follow various tutorials to get used to PHP's form handling, and general interaction with user input first before moving on.

We've also various PHP tutorials in our tutorials section (there is a sticky which links to the area) and a FAQ sticky in this section as well, feel free to ask any more questions as always :)

Alexander.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download