I have a PHP file (myFile.php) that parses XML, and assigns the data I need to a variable ($myVar). I need to echo this variable on an HTML page. I’ve tried adding <?php include ‘/myFile.php’; echo $myVar; ?> to the HTML file to no avail, however if I call the script itself (http://localhost/myFile.php) and add echo $myVar; to the end of the file the data is displayed as expected.
Am I missing something simple? I’m running PHP7 on a Linux Apache server - is there a setting somewhere that I need to change? I’ve used this syntax in the past without issue.
Thank you for the second set of eyes!
Try the following:
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/myFile.php';
echo $myVar;
?>
Related
In my web page, I wrote:
<?php
//define('__PUBLIC__', $_SERVER['DOCUMENT_ROOT'].'/public');
$doc_public = $_SERVER['DOCUMENT_ROOT'].'/public';
echo "Before include...<==============>$doc_public";
?>
<?php require_once($doc_public.'/inc/head.php'); ?>
<?php echo "After include...<==============>$doc_public"; ?>
And the page shows:
This firstly happened when I notice the fatal error in the footer, but the head is fine.
Although I can implement define or constant variable to avoid this, I am still curious how it happens.
P.S.: I run this under Apache with a port 8001. This is set in 【apache\conf\extra\httpd-vhosts.conf】. I am running more than one webapp under this site. I just share this information, as I am not sure this has anything to do with this case.
Thanks!
When you require a file, if a variable is modified it affects the original script as well, that's how it's designed. Require doesn't create a secondary environment separated from the including file, it just adds the PHP code in sequence, exactly like if you had written the code in the initial file.
Have a look at the official PHP documentation, the first example is exactly the same as your case
http://php.net/manual/en/function.include.php
(include is the same as require, the latter just throws an error. For more info about differences between include and require http://php.net/manual/en/function.require.php)
[[Fixed]]
Was accessing the file directly in my browser, instead of through the localhost web server
--
I'm fairly new to PHP and trying to get the following to work.
This code is in the middle of a HTML page.
I've initialized and set the variable right above the if statement
I'm checking if the variable is '1', when I've set it to '3'.
My problem is, that it is still running the html code in the IF statement when it shouldn't be
Any idea's?
Thanks in advance
<?php
$ad='3';
if($ad=='1')
{?>
<p>Broken</p>
<?php
}
?>
I found the problem.
You have created a HTML file and using php inside that which is totally wrong .
To get php code
working you need to create a php file and write php code
create a php file by saving a file as filename.php and copy those contents there, which will work fine
Also make sure that you have php installed on you system.If not download and install wamp server
You have to check the test field inside $_SESSION array! You are checking the whole array... :)
<?php
session_start();
$_SESSION['test']='2';
if($_SESSION['test']=='1')
{?>
<!--HTML Code-->
<?php
}
?>
You have also to check if the $_SESSION var is set:
session_start();
if(isset($_SESSION['test']))
{
if($_SESSION['test']=='1')
{
//do stuff HTML code
}
elseif($_SESSION['test']=='2')
{
//do other stuff HTML code
}
}
Let's begin with an article on a static page (Test.php) that includes another file full of PHP code (Code.php). Some of the echo values on Code.php are declared on a third file higher up the food chain (Values.php).
Everything works fine - until I take the article out of Test.php, insert it in a database and display it by echoing $Content. Now my include doesn't work, since you can't put PHP includes inside a database. (Or maybe you can, but it's apparently next to impossible, and everyone screams DON'T DO IT!)
I just learned how to use file_get_contents:
$Content = str_replace('<p id="1"', '.file_get_contents($BaseINC."/inc/Test.php").'<p id="1">', $Content);
It works great, except that it only displays static text - no PHP code.
Then I learned how to parse the file, like this:
file_get_contents("http://MySite/Test.php")
It works better. I can echo $Something, as long as $Something is defined in Test.php...
$Something = 'Cool!';
echo $Something;
The problem is that all the echo values that are defined on a separate file (e.g. Values.php) no longer work, apparently because I've removed Test.php from the flow. Is there a way to somehow reconnect Test.php with Code.php so those echo values will regain their values? Or is there some other way to accomplish what I'm trying to do?
For whatever it's worth, most of the missing values are created by a database query. One workaround is to recreate the values based on each page's URL. The irony is that all the scripts I've tried for displaying page URL don't even work; instead, they display the path to Test.php. So I'm really confused.
I tried to illustrate the different ways of including a php file:
<?php
//Test.php
$bar = 'orange'; echo $bar;
<?php
// Example.php
echo file_get_contents("Test.php") // $bar = 'orange'; echo $bar;
echo file_get_contents("http://example.com/Test.php") // orange
// Probably not correct, big security risk https://www.owasp.org/index.php/Code_Injection
include("Test.php") // orange
I'm working with PHP and HTML, but I have an issue popping up whenever I write some PHP code. An example of this is as follows:
<?php
echo "<h2>Hello?</h2>";
$var = 5;
echo "You have $var minutes to go.";
?>
What this ends up outputting on screen is:
Hello?"; $var = 5; echo "You have $var minutes to go."; ?>
But what I want to happen is this:
Hello? You have 5 minutes to go.
Is there something I'm forgetting to do? It doesn't seem to matter whether or not I add the HTML preamble, or if I put a tag like around the second echo line. Does anyone have any advice?
EDIT: Apparently I have failed to parse PHP correctly. This computer is new and I have XAMPP installed on it, but nothing else. Did I miss something I needed in order to use PHP?
That sounds like if the php code wasn't interpreted.
Make sure to have the code in a file with a filename ending with .php and that PHP is installed/enabled on your server.
The PHP isn't being parsed properly.
Make sure you are saving your files as .php
If you are running this locally through WAMP the make sure to use localhost in your URL because if your URL looks like this file:///C:/wamp/www/index.php then that is incorrect.
I think CakePHP uses .ctp files so that could also be an issue
You can setup Apache to interpret any file extension as PHP
make php and html different.so things become much easier.
try like this:
<h2>Hello?</h2>
<?php
$var = 5;
?>
You have <?php echo $var;?> minutes to go.
Hi All
I download a free chat script that in it's php files all of php code blocks start with <?php= instead of <?php or <? for print variables and constants that causes some problems and show error messages .
i want to know that how to solve this problem for php script work correctly
in your server's php config (php.ini file)
add
short_open_tag = On
At my understanding this <?php= is wrong..
Your currently problem is not the short open tags <?, but this code
<?=
means you are doing exactly this
<?php echo $someVar;
so all you have to do is change for the correct syntax.