/    Sign up×
Community /Pin to ProfileBookmark

Checking variables!

Hello!

I have a group of variables and I have to check if they are !=0:

$ene1t
$ene2t
$ene3t
$ene4t
$ene5t
$feb1t
$feb2t
$feb3t

As soon as one condition is true (!=0) I want to take the value of this one, doesn’t matter if more of them matches the condition, I only need the value of the first one.
How can I arrange that?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Daot_LagorilleFeb 19.2004 — Like this, I think:

BEcause you already have all your variables, and they already have values, you just need to define what is going to happen on the page in the event that one has a value not equal to zero (and it must catch the first one).
[code=php]
if ($ene1t != 0) {
[this happens]
}

else if ($ene2t != 0) {
[this happens]
}

else if ($ene3t != 0) {
[this happens]
}

else if ($ene4t != 0) {
[this happens]
}

else if ($ene5t != 0) {
[this happens]
}

else if ($feb1t != 0) {
[this happens]
}

else if ($feb2t != 0) {
[this happens]
}

else if ($feb3t != 0) {
[this happens]
}[/code]
Copy linkTweet thisAlerts:
@PerfidusauthorFeb 19.2004 — This is maybe more sophisticated and can show us if a variable matches condition, but what I need is to echo the first variable that matches. (It doesnt matter if 5 variables matches or not, just the first one that does)



<?php

$variables = array($ene1t, $ene2t, $ene3t, $ene4t, $ene5t, $feb1t, $feb2t,$feb3t );

for ($i =0; $i < count($variables); $i++)

{

if (!is_null($variables[$i]))

{

echo "some stuff";

break();

}

}

?>
Copy linkTweet thisAlerts:
@Daot_LagorilleFeb 19.2004 — Right. I am kind of new to php so I tend to do everything as simply as possible, which results in pages with tons of code, but it ends up being easier for me to debug. In this case, because you want to echo the value of the same variable that you are running the check on, I would just stick in the echo in place of where I write [this happens]:
[code=php]if ($ene1t != 0) {
echo $ene1t;
}
[/code]


And so forth. This will catch the very first one, in that order that you gave, that has a value not equal to zero, and display only it.
×

Success!

Help @Perfidus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...