I have a PHP echo like this, code below:
<?php echo($row['buildingdesignsvo']); ?>
How do I turn this into a If/Else based on the contents of the echo? If the echo for example is either value1 or value2 it displays a link like this <a href="">Link</a>
if it does not contain either value it displays Nothing found.
if ($row['buildingdesignsvo'] == 'value1') {
// do something
echo '<a href="">Link</a>';
} else {
// do something else
}
you should use ternary operator
echo ($row['buildingdesignsvo'] == 'value1')?( '<a href="">Link</a>'):();
I didn't test it, but somehow like this
I assume you will be echoing some value
echo $value
if($value==something){
echo 'this';
}else{
echo 'that';
}
<?php echo(in_array($row['buildingdesignsvo'], ['value1','value2'])? '<a href="">Link</a>': "Nothing Found" ); ?>
From your question I assume that you want to check to see whether $row['buildingdesignsvo']
equals 1 of two variables ( $value1
and $value2
) and then echo
"Nothing found" if not...
A simple if
statement:
<?php
if($row['buildingdesignsvo'] == $value1){
echo "<a href='link1.php'>Link 1</a>";
}
else if($row['buildingdesignsvo'] == $value2){
echo "<a href='link2.php'>Link 2</a>";
}
else{
echo "Nothing found";
}
?>
<?php
if($row['buildingdesignsvo'] == 'value1'){
echo '<a href="#">Link1</a>';
}
else{
echo 'default value';
}
?>
It might be better to use a function or even a switch case.
<?php
function check(){
if($row['buildingdesignsvo'] == 'value1'){
$data = '<a href="#">Link1</a>';
}
else{
$data = 'default value';
}
return $data;
}
$check = check();
echo $check;
?>
You don't actually want to do the if else on the contents of the echo, you want to do it on the contents of the array:
<?php
if ($row['buildingdesignsvo'] == $value1){
//If first value - Echo link
}
else if($row['buildingdesignsvo'] == $value2){
//If second value - Echo link
}
else{
//Doesn't match so echo 'Nothing Found'
}
?>
If the link is the same for both cases you can further simplify it by combining the first two cases with an OR
<?php
if (($row['buildingdesignsvo'] == $value1) || ($row['buildingdesignsvo'] == $value2)) {
//If first or second value - Echo link
}
else{
//Doesn't match so echo 'Nothing Found'
}
?>
Store it in a Var first, then check the value of it and echo it if so..
<?php
$var = $row['buildingdesignsvo'];
if ($var == 'value'){
echo $var;
}else {
//something to do;
}
?>
EDIT: after your edit :P
To check if it contains a string, use strstr() or stristr() for un-casesensitive.
<?php
$var = $row['buildingdesignsvo'];
if (stristr($var, 'value')){
echo "<a href=''>link</a>";
}else {
echo "not foundr";
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.