Solved

How do I check for the presence of an array?

Posted on 2016-09-14
6
45 Views
Last Modified: 2016-09-16
Here's the code as it exists currently:

 $statements = Statements::getQueuedSmsStatements($qty);
echo count($statements);
//if ($statements !==false){ //this was originally "=," rather than "==".
    if(!empty($statements))
     {

What you're looking at is a snippet of some code that I'm trying to troubleshoot. I'm "smelling" something foul with the third line down in that "if($statements!=false) looked problematic in that typically, with an IF clause, you always use two "=" signs.

Having said that, now I'm second guessing myself in that I'm testing for the presence of an array. And the matter become further convoluted when I run a code in the database that is populating the database with what the SELECT statement is going to be looking for that will result in an array with some values in it, yet...

I keep getting an "empty array" result with the code I have above.

Bottom line: I need to test for the presence of data in the Array. I've tried "count" as well as what you see above and I'm still not getting anything other than a "false" result.

What am I missing?
0
Comment
Question by:brucegust
  • 2
  • 2
  • 2
6 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41798857
You can use isset() and is_array() functions:


if (isset($statements) && is_array($statements)){
        if (count($statements) > 0){
                //do stuf here
        }
}

Open in new window

0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 150 total points
ID: 41798861
typically, with an IF clause, you always use two "=" signs
Why? You can check if a condition is true (==) of if it is false (!=) depending on your logic. Sometimes you use equal operators and ometimes you use identical operator (=== and !==) depending on the return value of the function, but it is not typical check for true with an if clause...
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 350 total points
ID: 41798904
if($statements!=false)

Open in new window

To expand on Marco's explanation

With code values like 1 TRUE "FRED" are called truthy values as they will evaluate to true
Values 0 NULL FALSE empty string, empty array are termed falsy values as they evaluate to false

What if you want to test whether a value is actually the value true or false.

That is where the === and !== come in

The statement in question is testing to see if the value returned by
Statements::getQueuedSmsStatements($qty);

Open in new window

is the value FALSE rather than an empty array or similar

Consider this code
<?php
$x = array();
if ($x) {
   echo "It are";
}
else {
   echo "It are NOT!";
}

Open in new window

The output from this is the "It are NOT!" - because $x is an empty array and evaluates to false.

The if statement is therefore distinguishing between the return of an empty array (which may be a valid success condition) and the value false which indicates a problem. If != had been used then the empty array would be misinterpreted as a false value whereas with !== the test is expressly for false
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:brucegust
ID: 41799049
Here's my dilemma:

I can run the SELECT in MSSQL Studio and get two rows, which is what I'm planning on.

I'll then go and run the syntax as I've got it written and get a "false" result. Hence, my hesitation, as far as whether or not I was using a proper IF clause.

My thinking is that I need to check for, not just the presence of an array, but whether or not that array has data in it. That being the case, from what I can gather from your collective input, is that I want something like:

$x=array();

if($x)
{
I've got values in the array
}
else
{
I may have a successful condition, but there's nothing in the array
}

Correct?
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 41799244
Yes - if you run that code I posted earlier you will see the result
At the risk of belabouring the point - here is another sample (working link below)
<?php
$data = isset($_POST['data']) ? $_POST['data'] : array();
if ($data) {
   echo "You sent me something, I am touched";
   echo "<pre>" . print_r($_POST, true) . "</pre>";
}
else {
   echo "Please send me something";
}
?>
<form method="post">
   Data <input name="data" type="text" /> <input type="submit" />
</form>

Open in new window

Working sample here
0
 

Author Comment

by:brucegust
ID: 41801783
Got it!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Undefined Index in HTML Form? 2 32
Store image url and display it for user profile 6 23
WordPress syntax 2 28
PHP Sessions in WordPress 8 9
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now