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?
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?';
you can try concatenation like
echo "<img src='".$url."' />";
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
?>
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';
}
?>
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
?>
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