Simple PHP file - php

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.

Related

file_get_contents does not work with MAMP

I have created an index.php page in MAMP.
My index.php reads exactly like the following. I access it through localhost:8888.
<?php
echo file_get_contents("http://stackoverflow.com");
?>
However, instead of returning the html source code from this page as I believe it would do, it just returns http://stackoverflow.com as a regular webpage, like the webpage you are looking at now.
My MAMP is using PHP 5.5.10. The user_agent is set and allow_url_fopen is on.
I am severely confused. I would very much appreciate any explanations :)
It IS returning the html and the browser is interpreting it.
You can try wrap the output in tags:
<?php
echo '<code>' . file_get_contents("http://stackoverflow.com") . '</code>';
?>
Or set headers as text/plain instead of html:
<?php
header('Content-Type: text/plain');
echo file_get_contents("http://stackoverflow.com");
?>
Or if you want to keep the headers and not inject the output into code tags:
<?php
echo htmlspecialchars(file_get_contents("http://stackoverflow.com"));
?>
I prefer the last one.
If you want to see the plain text you can use the following,
<?php
header('Content-Type:text/plain');
echo file_get_contents("http://stackoverflow.com");
?>
What you see in your version is correct, since the HTML is rendered by your internet browser.
The results of a php script are by default sent to the bowser, so your code
<?php
echo file_get_contents("http://stackoverflow.com");
?>
Is reading the web page and then sending it to your browser. So it looks like it is just showing the page you read.
If you change it to
<?php
$page = file_get_contents("http://stackoverflow.com");
?>
Then you can do something with the web page source stored in $page.

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)

PHP included functions not working in firefox 5

I have 2 files:
create.php:
<html>
<body>
<?php
require("Test.php");
hello();
echo "does this work?";
?>
</body>
</html>
and Test.php:
<?php
function hello(){
echo "hello";
}
?>
But when I open create.php, nothing prints (not even "does this work?". If I call hello() from Test.php it works fine. That is, it doesn't seem to be executing code after the include. What am I doing wrong?
edit: the code seems to work fine in my IE 8 install, but not in my FF 5 install (which, admittedly, has way to many addons).
edit again: the issue was that the page cache needed to be refreshed. There was never a problem. The code works. sorry, all.
Do yourself a favour and turn on error reporting. Place the following code at the beginning of create.php and let us know the error message(s) you receive.
<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL);
require_once('Test.php');
?>
My guess is that it is a path issue.
First of all, you do not need the html tags in your PHP-File. Second: you need to execute your function. Now you only defined it. Try
Test.php
<?php
function hello(){
echo "hello";
}
hello();
?>
And make sure that both files are in the same directory.
use dirname(FILE) instead of a stright include
eg
if i have a dir
/var/www/html/include
and test.php resides in includes and your script is in html then use dirname(__FILE__).'/includes/test.php
if you need to go back in a dir the use dirname(dirname(__FILE__))
depending on how many levels.
it also makes it dynamic so command line and browser will always fin the file
Try the code below for create.php
<?php
require('Test.php');
?>
<html><body>
<?php
hello();
echo 'does this work?';
?>
</body>
</html>

php in html, some part of code is not displaying

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 "

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.

Categories