echo is blank when using include statement - php

I wanted to try out an example you can find here: http://php.net/manual/en/function.include.php,
but I can't seem to be able to make it work.
In a first file, index1.php, I put this:
<?php
$color = 'green';
$fruit = 'apple';
?>
In the second file,index2.php, I put this:
<?php
include("http://www.domain.com/mypathtothefile/index2.php");
echo "A $color $fruit";
?>
It should echo 'A green apple', but it echos nothing.
The path is correct though, since when I put the echo part in the first file (index1.php), then it does echo 'A geen apple', both in index1.php as index2.php.
Hope someone can help.
Thanks in advance!

Shouldn't it just be:
include('index1.php');
Edit: corrected to reflect file names.

Related

How to echo php code and use it?

<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

output 2 string in one function

Ok, I must admit, I am a rather PHP noob who struggles with what seems to me a simple issue.
What i want to accomplish is that I can put 2 strings in 1 function and echo them in different places.
In common.php i have this:
<?
function printHeader($titel) {
?>
Later on in the file i echo it between
And in index.php i have this:
<?
include "common.php";
printHeader('this is my title');
?>
This works alright.... now what i like to do is to ad another String to the printheader to not only echo the title but also the H1, so i tried this:
Common:
<?
function printHeader($titel . $headtitle) {
?>
Index:
<?
include "common.php";
printHeader('This is my title!' . 'This is my H1');
?>
This does not seem to work. Are there any phpsavvy guys out here who can help me wit this simple problem?
If it is not to much to ask I would also like to Echo some standard value if $headtitle is empty, but that is waaaay of my league :)
EDIT: thanks to you guys the first problem is fixed. Now i want to try and fix the IF empty part. So I came up with this:
<?
function printHeader($titel, $headtitle) {
if (empty($headtitle)) {
echo 'title is empty'; }
?>
html goes here + this: <h1><?=$headtitle; ?></h1> more HTML
<? } ?>
This does not seem to work...
any help would be appreciated!
Thanks in advance,
A simple webdesigner ;)
Function arguments should seperate by a comma ,
function printHeader($titel , $headtitle)
And obviously same for calling,
printHeader('This is my title!' , 'This is my H1');
You have to seperate the second the argument by , like this
function printHeader($titel , $headtitle)
and change while calling also
printHeader("string1","String2");
or keep like this
function printHeader($titel){..... }
printHeader("string1"."String2");
so $title1 will have appended string.

Require found file, doesn't show String

First I'm sorry if this question is duplicated but I tried to find here the solution and spent some time reading some tutorials but I haven't found the solution.
I'm starting to learn PHP. I use Easy PHP 13.1 VC9.
In my testing project I have to files:
index.php:
<?php
global $str_texto;
echo 'before ';
require ('vars.php');
echo "$str_texto";
echo ' after';
?>
vars.php:
<? php
global $str_texto = 'String text';
$data_hoje = date('j / F / Y');
?>
When I start index.php my browser shows the page without errors (so I assume that EasyPHP found the file). It shows the words "before" and "after" but doesn't show the $str_texto string.
I compared my code to some code available on web like this available on PHP: Include - Manual :
vars.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
The same problem happens with that sample so, on my code, I changed the Include command to Require so I can get an error message. Same behavior and no error. There's a blank space between "before" and "after" were should be "String text". Declaring $str_texto as global and change quotation marks to single quotes didn't solve the problem.
I wasn't able to find any problem on web like this. Can you help?
There is a space in your php declaration in vars.php
<? php
Should be
<?php
Try this without quotes because is a variable:
echo $str_texto;
AND
change open-tag:
<? php
to
<?php
Try this:
var.php:
<?php
global $str_texto;
$str_texto = 'String text';
$data_hoje = date('j / F / Y');
?>
index.php:
<?php
global $str_texto;
echo 'before ';
require ('vars.php');
echo $str_texto;
echo ' after';
?>

Using php variables in included html

I'm looking for something much like the Using PHP variables inside HTML tags? question, but a little different.
In my case, I'd like to use code ore like this:
$somevar = 'a test';
include("file.html");
and file.html would contain
<b>hello, this is {$somevar}</b>
The problem is that it just prints hello, this is {$somevar}.
How can I make the HTML read the vars in the included file?
echo "<b>hello, this is {$somevar}</b>";
or
<b>hello, this is <?=$somevar?></b>
or
<b>hello, this is <?php echo $somevar; ?></b>
or
<b>hello, this is <?php print $somevar; ?></b>
You need to include the variable defining program, in the other program wanting to access it.
Example:
Say test.html has $somevar.
in file.html you do,
<?php
include('test.html');
echo "<b>hello, this is $somevar</b>";
?>
<?php
include "stuff.php";
$somevar = "test";
?>
<html>
<body><p><?php echo($somevar); ?></p></body>
</html>

Categories