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

laravel,how i can change the code to return an array?
How to make At Least Two Field Required to fill in Laravel 5.2
How do I allow PHP to run when the script is not in the URL?
How to check if a file is BEING used, i.e. some command is being executed on the data in that file?
Display profile data from database when login
How to integrate laravel packages into an application
Log into a Django project using Magento user database SSO
Image not show in recyclerview from server
php table exporting it to excel [duplicate]
regex not closing span
How to use GroupBy in nested query in Laravel 5?
Laravel Users table Update
Which classes can I type-hint dependencies for automatic injection in Laravel 5?
Urlmanager ignores everything before index.php
Sharing complex objects across controller methods Laravel 5.1
Query optimization for laravel pagination

Categories

HOME
android-fragments
postgresql-9.4
vsts-build
typeerror
websocket
docker-cloud
portable-class-library
mathematical-optimization
graphql-js
cocotb
hystrix
fasm
colors
bootstrap-switch
ex
cpu-usage
spam
psql
bluemix-mobile-services
connection-refused
ava
python-imageio
php-5.3
bitbake
d3.js-v4
wampsharp
osx-lion
kendo-listview
ms-media-foundation
char-pointer
express-session
unification
virtual-memory
transparency
nuclio
image-registration
icecast
knockout-2.0
md5-file
head.js
iptv
mura
scala-collections
http-basic-authentication
stateless-session-bean
case-when
hmmlearn
gapi
haraka
libharu
jsf-2.3
ftp-client
qtcpsocket
perforce-integrate
unsigned
uibinder
persistent-object-store
in-app-billing
fantom
subfolder
nanomsg
jcheckbox
zend-mail
inputbox
multivariate-testing
ecos
django-settings
notifyjs
postgres-xc
chartfx
html-frames
session-0-isolation
xalan
kaazing
play2-mini
bll
regression-testing
3-tier
oracle-pro-c
yahoo-maps
inversion
accumulator
index.dat
efs
stretchblt
chronic

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App