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.
Related
<?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".
database has php value saved like that as string
<?php $s = 'my name is'; ?>
and what am trying to do is calling the value as php code and want when type echo $s; to print my name is but now it's given my empty value see the code below to get what i mean
<?php
$b = '<?php $s = 'my name is';?>';
echo $b; //empty value
?>
Sounds like you're talking about eval() - but I'd be wary of using it. If you do, be extremely careful.
"If eval() is the answer, you're almost certainly asking the wrong question." -Rasmus Lerdorf
You'd probably need to strip the <?php and ?> tags, and watch for double quotes surrounding variables you don't want to replace:
$s=0;
eval('$s = "my name is";');
echo $s;
PHP is not recursively embeddable:
$b = '<?php $s = 'my name is';?>';
That's not PHP code in there. it's some text with the letters <, ?, p, etc...
And you ARE getting output. But you're viewing it in a browser, so the <?php ... ?> gets rendered as an unknown/illegal html tag and simply not displayed. If you'd bothered doing even the most basic of debugging, e.g. "view source", you'd have seen your PHP "code" there.
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
I'm looking to add an if statement to display a title if something is in a variable but not 100% sure how to go about it.
My current coding shows:
<?php echo $quote->getmessage(); ?>
But I would like it to show a title and the content of the message if there is content in the variable. If there is nothing within the variable I don't want to show anything.
IMHO it's better to be as verbose as necessary so that the code is easily readable and can be extended without totally rewriting it:
<?php
$message = $quote->getmessage();
if (!empty($message)) {
echo "Title!";
echo htmlspecialchars($message);
}
?>
Use php's isset function to check if a variable is set, and empty to see if it's empty.
For instance
<?php $a = $quote->getmessage();if (!empty($a)) echo $a; ?>
<?php echo ($quote->getmessage() == "") ? "" : "Title <br />".$quote->getmessage(); ?>
Use a ternary operator:
<?php
$quote_var = $quote->getmessage();
echo ($quote_var != null)?$quote_var:'NOTHING!';
//displays 'NOTHING' if the variable is null
?>
<?php
$mssg = $quote->getmessage();
echo (!empty($mssg))?$mssg:'';
?>
If you want your line to be concise, then I would advise this syntax:
<?php $msg = $quote->getmessage() AND print "<h6>title</h6>$msg"; ?>
The AND has a lower precedence than the assignment (but extra whitespace or braces make that more readable). And the second part only gets executed if the $msg variable receives any content. And print can be used in this exporession context instead of echo.
<?php
if(isset($quote->getmessage())
{
echo "My Title";
}
?>
I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.