Running PHP inside of PHP echo? - php

So I just found out that you can't run PHP inside of a PHP echo (yes silly of me)
So I can't think of an alternative way to run this script, perhaps making a variable?
It's a wordpress php script within a php script
<?php if ( !is_user_logged_in() ) {
echo 'Log in';
}
else {
echo 'Log out';
} ?>

Okay, there are few mistakes in this script.
First of all, you are using php tags inside php tags, it doesn't make sense, you are already using php so you don't need those php tags.
But even if you remove the php tags it won't work because you are inside a string, so you are asking php to write litteraly get_site_url() (you are not calling get_site_url(), you are litteraly writing get_site_url())
What should you do then ?
Lets see how concatenation work first. The concatenation operator is ".". It allows to concatenate two string.
Example :
$sentence = "Hello" . " " . "Thierry"; // means $sentence = "Hello Thierry".
Okay now lets do the same with variable.
$name = "Thierry";
$sentence = "Hello" . " " . $name; // means $sentence = "Hello Thierry";
This is everything you need here.
Lets see how we can solve your problem then,
what you want as a result is :
echo 'Log in';
Now we replace yourSiteUrl with the concatenation operator and the php function. And you have :
echo 'Log in';
Repeat the process and you'll end up with this :
<?php if ( !is_user_logged_in() ) {
echo 'Log in';
}
else {
echo 'Log out';
} ?>
Hope the english isn't too bad

Related

Run PHP code while echoing PHP

I'm using the echo command in PHP, but I want to enter PHP code like <?php echo $variable [id_login]?>, while echoing something else out, but this does not work.
Is this possible to do, and if so, how would I do it?
echo "<script>location='member.php?&id=<?php echo $taruh[id_login] ?></script>";
You cannot use echo or open/cloce php twice like you did, you might want to try something like the line below,
echo '<script>location=member.php?id=' . $taruh[id_login] . '</script>';
after echo you can write enything you'd like, even if it's a php variable, just use single quote and dot where you need it (like I do here), as you can see, echo is only used once..
For example:
<?php
$your_variable = 'some text';
$other_variable = 'some PHP code';
echo 'I wrote: ' . $your_variable . ' and ' . $other_variable . '!';
?>
Output will be:
I wrote: some text and some PHP code!
I hope this will bring you into the right direction..
EDIT
Also important: if you use query string in URLs, the first 1 can be a ? every other part after should be a & for example see the url below
http://www.example.com/index.php?id=12345&coder=yes&country=usa
before id I used a quest sign, for all others I didn't use the quest sign...

How to add a variable with a URL inside of an if statement

Here is the code I wrote. The else is working. The if URL's are not.
<?php
if ( is_user_logged_in() ) {
echo'Set As Facebook Cover';
echo'Download';
} else {
echo'Set As Facebook Cover';
echo 'Download';
}
?>
To include a variable in a string, the string needs to be wrapped in double quotes, like
echo "This is a $var";
not echo 'This is a $var';
Or you can use the . to concat the strings:
echo 'This is a ' . $var;
Try this:
echo'Set As Facebook Cover';
echo'Download';
Final note, there is no need to use the ; after the variables because you are not ending the statement.
See a demo

PHP script inside of string?

Is this possible?
What I am trying to accomplish:
Create a html template in the form of a string
Inside the string, add a script, something like include 'php/countries.php';
Echo entire string to html page
Everything but the 2nd step works. I would like to see a php file echo the question being asked, onto the html page, including an echo from another php file.
EXAMPLE
echo "<div id=\"first\"><?php include \'countries.php\'; ?></div>";
I have tried the above, as well as the below:
EXAMPLE
echo "<div id=\"first\">".include 'countries.php'."</div>";
Would this require eval?
Any and all help is appreciated.
Seems a bit silly, but you could do the following:
echo "<div id=\"first\">" . file_get_contents('countries.php') . "</div>";
Or...
echo "<div id=\"first\">";
include "countries.php";
echo "</div>";
Or...
$externalfile = compileexternal('countries.php');
function compileexternal($file) {
ob_start();
require $file;
return ob_get_clean();
}
echo "<div id=\"first\">" . $externalfile . "</div>";
If none of these are what you need, please update the question. There are a dozen ways.
You can use
eval()
But it is not a good practice.
You can use a regular expression.
For example, your string could be;
<div id="first">{{countries.php}}</div>
You'd then do;
$string = "<div id='first'>{{test2.php}}</div>";
echo preg_replace_callback("/(\{\{.+\}\})/", function($matches) {
include_once( str_replace(array("{", "}"), "", $matches[0]));
}, $string);
Check the file exists if( file_exists() )
Check the file can be included (we don't want to include ../../../../../etc/passwd

How to pass a PHP variable using the URL

I want to pass some PHP variables using the URL.
I tried the following code:
link.php
<html>
<body>
<?php
$a='Link1';
$b='Link2';
echo 'Link 1';
echo '<br/>';
echo 'Link 2';
?></body></html>`</pre></code>
pass.php
<pre><code>`<html>
<body>
<?php
if ($_GET['link']==$a)
{
echo "Link 1 Clicked";
} else {
echo "Link 2 Clicked";
}
?></body></html>
Upon clicking the links Link1 and Link2, I get "Link 2 clicked". Why?
In your link.php your echo statement must be like this:
echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo 'Link 2';
Then in your pass.php you cannot use $a because it was not initialized with your intended string value.
You can directly compare it to a string like this:
if($_GET['link'] == 'Link1')
Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.
You're passing link=$a and link=$b in the hrefs for A and B, respectively. They are treated as strings, not variables. The following should fix that for you:
echo 'Link 1';
// and
echo 'Link 2';
The value of $a also isn't included on pass.php. I would suggest making a common variable file and include it on all necessary pages.
I found this solution in "Super useful bits of PHP, Form and JavaScript code" at Skytopia.
Inside "page1.php" or "page1.html":
// Send the variables myNumber=1 and myFruit="orange" to the new PHP page...
Send variables via URL!
//or as I needed it.
<a href='page2c.php?myNumber={$row[0]}&myFruit={$row[1]}'>Send variables</a>
Inside "page2c.php":
<?php
// Retrieve the URL variables (using PHP).
$num = $_GET['myNumber'];
$fruit = $_GET['myFruit'];
echo "Number: ".$num." Fruit: ".$fruit;
?>
All the above answers are correct, but I noticed something very important. Leaving a space between the variable and the equal sign might result in a problem. For example, (?variablename =value)
Use this easy method
$a='Link1';
$b='Link2';
echo "Link 1";
echo '<br/>';
echo "Link 2";
You can do this like this:
<a href="./gemsPack.php?uid=<?php echo $id; ?>&amount=1.99">
just put
$a='Link1';
$b='Link2';
in your pass.php and you will get your answer and do a double quotation in your link.php:
echo 'Link 1';

Display dynamic text based on GET parameters

I'm trying to learn PHP, and I figured I'd make myself a simple exercise of making a site that if someone goes to it, they get "Hello friend!" but if my wife (who is named Dawn) goes to it, she gets a different message.
Unfortunately, it's always showing up as blank and I'm not really sure why.
I know it works for index.html with just text, and I know it works for index.php as long as I have no <?php tag in it (just text works). But when I try to make it actual php, it just fails.
I'd like site/index.php to yield
"Hello friend!"
I'd like site/index.php?who=Bob to
yield "Hello friend!"
I'd like site/index.php?who=Dawn to
yield "Hello Dawn! I love you!"
Here's what I have:
<?php
print 'Hello ';
$who = $_GET("who");
if($who && $who == "Dawn")
print "Dawn! I love you!";
else
print "friend!";
/>
So, what's wrong?
Access to arrays ($_GET is an array), like in Java, uses square brackets:
$who = $_GET['who'];
Also if($who) evaluates to true if $who is non-false, to check it's set you need to use isset:
if(isset($who) && $who == "Dawn")
Last, as noted by #Shivan, the end tag should be ?>, not />.
Multiple issues, should be:
<?php
print 'Hello ';
$who = $_GET["who"];
if(isset($who) && $who == "Dawn") {
print 'Dawn! I love you!';
} else {
print 'friend!';
}
?>
More information:
if possible, use single quotes for faster parsing
it is a good habit to close if else case with brackets
end tag should be a ?>
try this on for size:
<?php
echo 'Hello ';
$who = isset($_GET["who"])?$_GET["who"]:false;
if($who)
echo "Dawn! I love you!";
else
echo "friend!";
?>
This checks to make sure there is a _GET value with key who or else php will throw errors.

Categories