php


Simplified anarchy “if statement”


Is there a way to code this function/executable..
if($img1 > "")
{
$show = $img1;
}
elseif($img2 > "")
{
$show = $img2;
}
elseif($img3 > "")
{
$show = $img3;
}
else
{
$show = 'default.jpg';
}
echo $show;
..in a more simple way? Thx.
Any time you end up using variables named 1,2,3,etc it means you should be using an array.
If you had it like this, for example:
$img = [
1=> '',
2=> '',
3=> 'image.jpg'
];
You could then process this like so:
// Remove empty values
$img = array_filter($img);
// Echo the first value found (image.jpg)
echo current($img);
Or to add your default:
echo (count($img)) ? current($img) : 'default.jpg';
This uses a ternary operator to echo 'default.jpg' if count($img) is 0.
If you know the id number of the image, you could do something like this:
<?php
$images = array(
'myimg.jpg',
'my_awesome_img.jpg',
'awesome_awesome.jpg'
);
$show = $images[$x];
?>
You can just do
if(!empty($img1)) {
echo $img1;
} elseif(!empty($img2)) {
echo $img2;
} elseif(!empty($img3)) {
echo $img3;
} else {
echo 'default.jpg';
}
given that you only ever want to display one of these.
Did not test, but something like this:
<?php
$img1 = 'hello';
$img2 = 'world';
$array = [1,2,3];
for($i=1; $i<count($array); $i++){
$v = ${"img{$i}"};
if( !empty($v) ){
$show = $v;
break;
}
}
echo $show;
No idea what you're trying to accomplish or why the code looks like that, seems poorly constructed from the get-go.

Related Links

PDO and nested fetching
Unpredictable log file writing in PHP
Get seconds until end of day in PHP
Validating XML from Twitter
linux file size
Session handling in PHP
How do you use PHPUnit to test a function if that function is supposed to kill PHP?
I have a php form that I want propogated with the current logged in Microsoft Active Directory Real Name
Deleting from PHP array and having indexes adjusted
php source code to PO file generator
Manipulate a string that is 30 million characters long
Only a DB Class or a Combo Group (DB Class + Results Class)
How can I separate SQL from my PHP?
Query displays first entry in table only - wrong!
inserting a array of values
Php object orient concept in file handling

Categories

HOME
flask
powerbi
openxml
focus
softlayer
hid
filesize
website
odata
capistrano
javafx-8
heuristics
colors
max-msp-jitter
c#-7.0
android-contacts
jaspersoft-studio
raml
chronicle-queue
ratio
nurbs
python-appium
fstar
code-climate
sparkle
microprocessors
dnsmasq
unmarshalling
mdns
phpstorm-2016.1
premake
windows2012
custom-keyboard
directfb
bootstrap-sass
polyml
des
keyboard-layout
subclassing
redgate
ftp-server
djcelery
swfupload
netapp
plane
virtual-memory
sendinput
zend-form
asihttprequest
docker-ucp
django-static-precompiler
entropy
network-protocols
dex
predicates
word-2016
http4s
adobe-reader
email-injection
raw-sockets
wolfram-language
timestamping
deeplink
hmisc
whitespace-language
password-recovery
configurable-product
datagridcomboboxcolumn
adwords-apiv201402
csslint
incognito-mode
fileshare
usb-flash-drive
windows-taskbar
mft
saga
san
mongo-jackson-mapper
ofstream
lynx
cgrectmake
ip-geolocation
clrprofiler
firebird1.5
microblogging
net-use
asp.net-mvc-views
ios-4.2
tabbarcontroller
version-control-migration
stretchblt
error-detection

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App