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>
Related
Hello let me explain my question. I'm using PHP at the moment and I'm just playing around a bit now im wondering if I have a file for example:
//This is file1.php
<?php
$text = "Hello";
?>
And include it in file2 and also include file3:
//This is file2.php
<?php
include 'file1.php';
include 'file3.php';
?>
Now Comes my question can I use $text in file3 like so?:
//This is file3.php
<?php
echo $text;
?>
Thank you for your response!
Short ans quick: Yes you can, but NOT in your example. If you call $text in your file2 after including it, it works.
Include does the same, as you have the code in the same file. But look at the order, its important.
Build one file you call over the browser and require all you need in this file.
I am very new to PHP programming. I am trying to write this custom php code in Drupal and seeing this weird behavior. Basically I have two php files which users can hit and the first php file is showing output from the second one. I am not including (include) the second file in the first one.
Home.php - The first file (outputs 'why am i executing' at the end)
<?php include 'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
include 'db.inc';
include 'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php - The second file
<?php
include 'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
include 'db.inc';
include 'dao.inc';
?>
Apart from the output from the second file, I am also seeing this error - Cannot redeclare class IDA_Map.
I have read in other posts about 'include_once' but didn't expect re-declaration error since the class (IDA_Map) is declared once per request.
Thank you.
Flip all these to require_once.
<?php require_once'HomeView.BL.inc';
//Other links to access second file
?>
HomeView.BL.inc
<?php
require_once'db.inc';
require_once'dao.inc'; - has a class called IDA_Map
?>
FacultyInternshipDetail.php
<?php
require_once'InternshipDetailView.BL.inc';
?>
InternshipDetailView.BL.inc
<?php
echo "why am i executing";
require_once'db.inc';
require_once'dao.inc';
?>
include breaks when you try to include the same file twice.
Also look up composer and into using a php framework so you dont have to worry about any of this!
I had document addsite.php like below.
<?php
session_start();
if(condition){
///Include this document
include_once('sub_docs/addsite.php');
}
else {
die(header("author"));
exit();
}
?>
And my addsite.php in sub_docs is an combo of both HTML and PHP as below
<!DOCTYPE>
<html>
....
....
<?php
?>
....
....
</html>
Everything is working fine in my local server but when went for production it is just showing the blank page. Not even a single markup tag. It is even entering the conditions and just stopping there. Neither of the die or include_once are working. There is no problem with any sql statements or the php script in between the html. I can't find the mistake. Please help.
Try turning on some error reporting so you can see what's happening:
error_reporting(-1);
ini_set('display_errors', 'On');
Also - make sure your secondary page is a .php file and not an .html, unless of course you have it setup in your .htaccess file to allow .html to run as php.
<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.
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.