php in html, some part of code is not displaying - php

I would like to learn PHP and started reading this info's in the website: And I have question about this code:
Why is this line not displaying? " echo 'Neo: I am Neo, but my people call me The One.';"
Thanks
<html>
<head></head>
<body>
Agent: So who do you think you are, anyhow?
<br />
<?php
// print output
echo 'Neo: I am Neo, but my people call me The One.';
?>
</body>
</html>

You need a webserver, and give it a .php extension.
You cannot run it directly from your drive (e.g. /home/user/file.php or C:\file.php), you must run it from your server (e.g. http://localhost/file.php or http://example.com/file.php)
A webserver can be downloaded from http://www.xampp.org/

Make sure you are using a virtual webserver such as mamp or xampp,
Also try print instead of echo with "

Related

Embed PHP into HTML

I'm trying to use PHP to print out values for my select statements but I can't get it to work. I've even tried just to print something out in my html with php, but i can't even seem to do that. Can anyone tell me what's wrong with this
<html>
<head></head>
<body>
<ul>
<?php echo "register Student:"; ?>
</ul>
</body>
</html>
Make sure you save your file as .php not .html.
myfile.php:
<html>
<head></head>
<body>
<ul>
<?php echo "register Student:"; ?>
</ul>
</body>
</html>
You will also need to run this file on a server that has php installed. It won't run directly in the browser like an .html file will.
Keep these things in mind.
Your file name should always end with .php extension not .htm or .html
You should always run file on localhost/ where you set up your environment
Please follow the standard of W3C Use a "li" in you "ul"
Hope it works.
Name your file whatever.php
Make sure it is NOT whatever.html
If that doesn't work, be sure to check whether or not you installed php. I suggest a local testing ground such as XAMPP. If you don't want to do that, upload it to a free or paid hosting service.

How to call a php function in HTML

Rookie here, so please correct me if I have anything wrong.
So here's a snippet of my HTML:
<html><body>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
</body></html>
The function, $fgmembersite->UserFullName(), returns a string (100%, if I call the function within tags it prints out correctly). How do I get it to echo out in the HTML?
I think an alternative would be to echo the entire HTML code, and I think it'd work then, but I don't want to do it that way because I read somewhere that echoing all of your HTML is bad. Could somebody also confirm/deny that?
Right now, this is what shows on the site:
Welcome back UserFullName(); ?>!
No idea why
Thanks for your time!
I think you want the code to look like this...
Welcome back <?php echo $fgmembersite->UserFullName(); ?>!
This will ECHO the result of the function call.
What you are seeing is the result of the browser trying to parse the PHP. It is trying to treat <?= $fgmembersite-> as an unknown tag and then renders the rest as text.
There are two possible reasons for this:
You need to pass it through a PHP engine first.
You need to access the file through a web server which supports PHP and is configured to run your file through PHP when it is requested (typically this is done by using a .php file extension).
Note that installing a web server then double clicking a PHP file in your file manager isn't sufficient - the browser will just load the file from the file system. You need to type http://etc etc.
It is also possible that you have short_open_tag disabled (which is common and sensible) and are using PHP 5.3 or older (which isn't a great idea, 5.3 is still supported but it is the oldest branch that is).
If this is the case, your options are:
Upgrade PHP
Use <?php echo ... ?> instead of <?= ... ?>
Enable short_open_tag
Can't you just do
<?php echo $fgmembersite->UserFullName(); ?>
?
use it as follows:
<html><body>
<h2>Home Page</h2>
Welcome back <?php echo $fgmembersite->UserFullName(); ?>!
</body></html>
Do you have short_open_tag enabled on your server? You need to have that enabled to use that syntax. If you don't have that, use <?php echo $fgmembersite->UserFullName(); ?> like the other users suggested.
You can check the setting on you server by creating a file containing <?php phpinfo(); in it, and accessing that.
Also, echoing all your HTML inside the <?php tags are considered a bad practice because you should separate your business logic and your HTML. Echoing HTML directly from inside the PHP tags quickly leads to a mix of HTML and PHP, and it will be harder to read (for others, and yourself later on)

Extra output in for php in html hello world file

I'm creating a LAMP stack and just started learning PHP. I'm having trouble with my Hello World program. On my server I have a file called index.html. This is the contents of that file (which is basically copy pasted from an online guide):
<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
echo '<p>Hello World</p>';
?>
<b>Here is some more HTML</b>
</body>
</html>
When opened on a web browser, I expect my output to look something like this:
My Example
Hello World
Here is some more HTML
Instead, it looks like this:
My Example
Hello World
'; ?> Here is some more HTML
Why is that extra "'; ?>" there? I'm probably making a simple mistake. I've tried accessing index.html on chrome, firefox, and safari, same result each time.
PHP does not work in HTML files, try renaming it to index.php
Apart from that the code is fine (maybe some best practices like separating HTML and PHP, but you'll get to that later)
The reason you don't see the whole PHP code on your screen is because the browser is trying to parse the element <?php *** ?> as if it where valid HTML (like <span>). If you check the html-source, you will see all of your code.
As someone in the comments below mentioned, there are ways to make PHP work in HTML. You should not do this, unless you are very aware of what you are doing. It is not a default setting and it should stay that way. If you should be able to use PHP in HTML files, .phpfiles would not have to exist.
When you expand your knowledge, you will start separating HTML files from PHP files and make PHP include templates. Two separate files for two separate goals
Rename the file to index.php.
In addition to this, the PHP code must be interpreted by a WEB SERVER(Apache in your case).
For example(if you are using XAMPP on Windows), and your php file is present in the webcontent directory of your server's document root(htdocs), then type the following in your browser
http://localhost/htdocs/webcontent/index.php.
Like others said, you must rename index.html to index.php, but if you must use index.html, put the line below into your .htaccess file, so you can process .html files as .php.
AddType application/x-httpd-php .html

Learning php. Hello world won't display using easyphp and writing in notepad++

<html>
<!--HTML-->
<head><title>a quick test</title></head>
<body>a quick test</body>
<p>javascript</p>
<!--javascript-->
<p><script>
document.write("hello world")
</script></p>
<p>php</p>
<!--php-->
<?php
Echo "hello world";
?>
</html>
The Hello world works for javascript but not in php, what gives? Any suggestions or obvious errors?
thanks
Are you saving this file with a .php file extension? PHP code will not execute within a normal .html file.
If you view source, can you read your PHP?
Perhaps your environment is set up wrong.
What does <?php phpinfo(); ?> yield?
To be w3 valid by the way, your body tag should end before the ending html-tag -> as such:
<body>a quick test
<p>javascript</p>
<!--javascript-->
<p><script>
document.write("hello world")
</script></p>
<p>php</p>
<!--php-->
<?php
Echo "hello world";
?>
</body>
</html>
First of all, before saving any file related to PHP, You have to clear see in which format it is going to Save. Save the file in .php extension, for example : filename.php then execute it at WAMP server. You will find HELLO World.

Simple PHP file

Here is my code
<html>
<body>
<?php
echo "<b>Hello World</b> <br />";
?>
</body>
</html>
I have named the file as test.php but I dont get the desired output in my firefox 3.6 browser.
Output
Hello World
"; ?>
Sounds like you haven't configured PHP properly. Refer to the PHP documentation and the documentation for your web server.
Have to copied the file to a server with PHP installed? If you just try to open the file in Firefox, it'll just trying to display the whole thing as HTML, which isn't going to do what expect.
In a page called index.php put this code:
<?php phpinfo(); ?>
and see what happens.

Categories