Simple php really, just can't get it for some reason...
<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits
<?php echo 'Test'; ?> and an if statement
<?php if ( $anothervalue = "test" ) { echo 'Print this'; } else
{ echo 'Print that' ; } ?>
And then some more content
<br />
Test
?>
} else {
echo 'The value didnt match';
}
?>
I need to include the php inside the echo, how can i do this?
As far as I know, you can't. echo is just for outputting.
Refactor instead.
<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
?>
This is some test content, within which is some more php bits <?php echo 'Test'; ?> and an if statement
<?php
if ( $anothervalue = "test" ) {
echo 'Print this';
} else {
echo 'Print that' ;
}
?>
And then some more content
<br />
Test
<?php
} else {
echo 'The value didnt match';
}
<?php
function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; }
else { return 'Print that' ; }
}
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content
?>
I would suggest that you create a function first:
<?php
function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; }
else { return 'Print that' ; }
}
then include it in your statement/text;
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content
?>
You have to concatenate strings bits with the . operator:
echo 'This is some test content' . ' chained to other stuff' . someFunction() . 'and some other stuff' . $someVariable . ', right?';
try this
<?php
$myvalue = 'yes';
if ( $anothervalue == "test" ) { $test = 'Print this'; } else { $test = 'Print that' ; }
if ( $myvalue == 'yes' ) {
echo "This is some test content, within which is some more php bits $test
<br />
Test";
}
else
{
echo 'The value didnt match';
}
?>
You can certain express conditional text in the context of an echo. You can use string concatenation (using commas or dots) with inline conditional syntax (ternary operators) so that you don't have to bounce in and out of <?php nor would you need to write multiple echos.
Code: (Demo)
$myvalue = 'yes';
$anothervalue = 'test';
if ($myvalue == 'yes') {
echo 'This is some test content, within which is some more php bits Test ' , ($anothervalue == 'test' ? 'Print this' : 'Print that') , ' And then some more content<br />Test';
} else {
echo 'The value didnt match';
}
Output:
This is some test content, within which is some more php bits Test Print this And then some more content<br />Test
you can try concatenation like
echo "<img src='".$url."' />";
Try this but I suggest you go to w3schools and learn up on the basics before continuing.
<?php
$myvalue = 'yes';
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits Test and an if statement';
if ( $anothervalue = "test" ) {
echo 'Print this';
}
else {
echo 'Print that';
}
?>
And then some more content
<br />
Test
<?php
}
else
{
echo 'The value didnt match';
}
?>
Related
I am trying to display a different image on my page depending on who the Wordpress author of the post is.
So far I have tried a few scripts but none of them work. Any help is greatly appreciated. Here is what I am trying to do.
<?php $author = get_the_author(); ?>
<?php
if ( $author('author1') ) {
echo '
<img src="">;
'
} elseif ( $author('author2') ) {
echo '
<img src="">;
'
} else {
// if neither, echo something else
}
?>
The get_the_author() function return the author's display name as a string. So you have to simply compare the result of get_the_author(). There is no array or object as return value.
So I would go with the following solution using a switch instead of if:
<?php $author = get_the_author(); ?>
<?php
switch($author) {
case 'author1':
echo '<img src="">';
break;
case 'auhtor2':
echo '<img src="">';
break;
default:
// if neither, echo something else
}
?>
In case you want to use the if statement you can use the following:
<?php $author = get_the_author(); ?>
<?php
if ($author === 'author1') {
echo '<img src="">';
} elseif ($author === 'author2') {
echo '<img src="">';
} else {
// if neither, echo something else
}
?>
So basically what am trying to achieve is the following.
I am trying to make it so the following script does something in this instance:
If $something == "0" then $something1 == "no"
If $something == "1" then $something1 == "yes"
else echo "Error."
That is how I would explain what Im trying to do.
This is my current code:
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
?>
I would like it to echo "no" if the result of array "something" is "0" and echo "yes" if the result of array "something" is "1".
<?php
if($array['something'] == '0'){echo 'No';}
elseif($array['something'] == '1'){ echo 'Yes';}
else{ echo 'Error!'; }
?>
switch case is the most elegant way to go here:
switch($array['something']) {
case 0: echo 'No';break;
case 1: echo 'Yes';break;
default: echo 'Error.';
}
<?php
if(isset($_POST['resolve'])) {
$api = "http://test.com/php/";
if(!$_POST['name']) {
echo "Please, fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
echo "<div align='center'>";
if($array['something'] == '0') {
echo 'No';
}
elseif($array['something'] == '1') {
echo 'Yes';
}
else {
echo 'Error.';
}
echo "</div>";
}
}
?>
Don't forget to also do a security input check on $_POST['name']
Voila
echo $array['something1'] ? "Yes" : "No";
This sets $array['something1'] to either 'yes' or 'no' depending on the value of $array['something'].
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
$array['something1'] = $array['something'] == 0 ? 'no' : 'yes';
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
$yesno = ['No', 'Yes'];
$something1 = $yesno[$something];
That is the simplest way I know of doing it.
Please have a quick look at the code below:
<?php
$pagexfoot = $_GET[page_id];
?>
<?php
if ($pagexfoot == '5' OR !isset($_GET['page_id'])) {
echo 'Hello';
} else {
echo 'Bye';
}
?>
So, if the user is on index.php?page_id=5 then it will echo "Hello" and it will echo "Bye" anywhere else. Now, how do I echo "Hello" on page index.php?page_id=5 and index.php and echo "Bye" on all other pages? Who can solve this puzzle...
<?php
if(isset($_GET['page_id']) && $_GET['page_id'] != 5)
{
echo 'Bye';
}
else
{
echo 'Hello';
}
?>
<?php
if (!isset($_GET['page_id']) || $_GET['page_id'] == 5) {
echo 'Hello';
} else {
echo 'Bye';
}
We are using the || operator to check if either it isn't set or the value is 5, if so tell "Hello" and else, Bye.
$pagexfoot = $_GET[page_id];
if($pagexfoot != '5' || isset($_GET['page_id'])) { echo 'Bye'; } elseif($pagexfoot == '5') { echo 'Hello'; }
You code loks fine to me by try the above code :)
I have a variable that is posted through a html form:
$_POST['ref']
And a variable that is pulled from a table in a database:
$row['ref']
i have a basic comparison script to check if they are both the same:
$ref = $_POST['ref'];
$result = mysql_query("SELECT * FROM logbook.job");
if (!$result) {
die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result);
$refdb = $row['ref'];
$refform = $_POST['ref'];
echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>";
if ($refdb == $refform) {
echo "Yes they are<br><br>";
}
else {
echo "No they are not<br><br>";
}
if (is_string($_POST['ref']))
{
echo "Yes";
} else {
echo "No";
}
echo "<br>";
if (is_string($row['ref']))
{
echo "Yes";
} else {
echo "No";
}
Which outputs:
G2mtxW
G2mtxW
No they are not
Yes
Yes
I echo them both out. Than i ask if they are the same. Then i check whether each is a string.
How come they are not the same? How can i get them to match
Any help would be appreciated
Try using the binary-safe comparison for String:
result = strcmp($str1, $str2);
If the result is 0, then both are the same. Otherwise, they aren't.
One of your strings (probably the one from the DB) might be null-terminated. I've tested the following
$foo = "abc\0";
$bar = "abc";
echo "$foo\n$bar\n";
if($foo == $bar)
echo "Equal.";
else
echo "Not equal."
Output is
abc
abc
Not equal.
Try var_dump-ing both values, check their lengths and inspect them using view-source. They are different in someway.
echo $status_message;
echo "Accepted";
if(strcmp($status_message,"Accepted")==0)
{
echo "equal";
}
else
{
echo "not equal";
}
?>
$row['status'] is a field from table
<?php if (!empty($box1) && !empty($box2)) { echo ' | content here'; } ?>
<?php if (!empty($box1) && empty($box2)) { echo 'content here'; } ?>
Basically, I want to get rid of the pipe if box2 is empty. Is there a better way to write this?
<?php if (!empty($box1)) { echo (empty($box2) ? '' : ' | ') . 'content here'; } ?>
It's hard to say what's the "best" way to write it elegantly without having a grander scheme of things, but at least you can shorten it as follows:
<?php if(!empty($box1)) { echo (!empty($box2) && ' |') . 'content here'; } ?>
Alternately, if you don't like the && style, you can use a ternary operator:
<?php if(!empty($box1)) { echo (!empty($box2) ? ' |' : '') . 'content here'; } ?>
Or another conditional.
Coarsely, if "most" elegant way would be to take a closer look at what $box1 and $box2 represent and then create a view helper (in the MVC methodology) along the lines of:
class SomeModel {
int $box1;
int $box2;
function make_suffix() {
$suffix = '';
if(!empty($this->box1)) {
if(!empty($this->box2)) {
$suffix .= ' | ';
}
$suffix .= 'content here';
}
return $suffix;
}
}
<?php
if (!empty(&box1)) {
if (!empty($box2) {
echo ' | ';
}
echo 'content here';
}
?>
Using just ternary operators:
<?php echo !empty($box1) ? ( !empty($box2) ? ' | ' : '' ) . 'content here' : '' ?>