php if both are empty

I want to say if both items are not empty. I have tried below, but it doesn't work?

if (!empty($row_rs_customer_selected['bespoke_pricelist']) && !empty($row_rs_customer_selected['ink_bespoke_pricelist'])) {
 // Show contents
}

Open in new window


$row_rs_customer_selected['ink_bespoke_pricelist'] is not empty however the contents is not being shown.

What have I done wrong?
petewinterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Maybe you want to test for empty() instead of not-empty?  Maybe you want to test for "or" instead of && condition.
0
petewinterAuthor Commented:
No I want to check if they are both NOT empty. Have I done anything wrong?
0
Julian HansenCommented:
I am going to go with "one of those values is empty"
Consider this code
<pre>
<?php
$row_rs_customer_selected = array (
  'bespoke_pricelist' => 'not empty',
  'ink_bespoke_pricelist' => 'not empty'
);

echo "Both not empty<br/>";
if (!empty($row_rs_customer_selected['bespoke_pricelist']) && !empty($row_rs_customer_selected['ink_bespoke_pricelist'])) {
  echo "  => Show contents\n";
}
else {
  echo "  => At least one is empty\n";
}

echo "Both ARE empty\n";
$row_rs_customer_selected = array ();

if (!empty($row_rs_customer_selected['bespoke_pricelist']) && !empty($row_rs_customer_selected['ink_bespoke_pricelist'])) {
  echo "  => Show contents\n";
}
else {
  echo "  => At least one is empty\n";
}

echo "One is empty the other is not\n";

$row_rs_customer_selected = array (
  'bespoke_pricelist' => 'not empty'
);

if (!empty($row_rs_customer_selected['bespoke_pricelist']) && !empty($row_rs_customer_selected['ink_bespoke_pricelist'])) {
  echo "  => Show contents\n";
}
else {
  echo "  => At least one is empty\n";
}
?>
</pre>

Open in new window

Output
Both not empty
	=> Show contents
Both ARE empty
	=> At least one is empty
One is empty the other is not
	=> At least one is empty

Open in new window

The logic is sound therefore the problem lies with your inputs.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Ray PaseurCommented:
Please see http://iconoun.com/demo/petewinter.php
Man page: http://php.net/manual/en/function.empty.php
<?php // demo/temp_petewinter.php

/**
 * http://www.experts-exchange.com/questions/28714321/php-if-both-are-empty.html
 */
error_reporting(E_ALL);
echo '<pre>';

// SET UP THE TEST DATA - BOTH EMPTY
$row_rs_customer_selected['bespoke_pricelist']     = NULL;
$row_rs_customer_selected['ink_bespoke_pricelist'] = NULL;
if (empty($row_rs_customer_selected['bespoke_pricelist']) && empty($row_rs_customer_selected['ink_bespoke_pricelist']))
{
    var_dump( $row_rs_customer_selected['bespoke_pricelist'], $row_rs_customer_selected['ink_bespoke_pricelist'] );
    echo 'BOTH ARE EMPTY' . PHP_EOL;
}

// SET UP THE TEST DATA - ONE EMPTY
$row_rs_customer_selected['bespoke_pricelist']     = NULL;
$row_rs_customer_selected['ink_bespoke_pricelist'] = TRUE;
if (empty($row_rs_customer_selected['bespoke_pricelist']) || empty($row_rs_customer_selected['ink_bespoke_pricelist']))
{
    var_dump( $row_rs_customer_selected['bespoke_pricelist'], $row_rs_customer_selected['ink_bespoke_pricelist'] );
    echo 'AT LEAST ONE IS EMPTY' . PHP_EOL;
}

// SET UP THE TEST DATA - NEITHER EMPTY
$row_rs_customer_selected['bespoke_pricelist']     = TRUE;
$row_rs_customer_selected['ink_bespoke_pricelist'] = TRUE;
if (!empty($row_rs_customer_selected['bespoke_pricelist']) && !empty($row_rs_customer_selected['ink_bespoke_pricelist']))
{
    var_dump( $row_rs_customer_selected['bespoke_pricelist'], $row_rs_customer_selected['ink_bespoke_pricelist'] );
    echo 'NEITHER IS EMPTY' . PHP_EOL;
}

Open in new window

0
petewinterAuthor Commented:
If I do the below it works...

$bothempty = $row_rs_customer_selected['bespoke_pricelist'] . $row_rs_customer_selected['ink_bespoke_pricelist'];

if (!empty($bothempty)) { 
// Show contents
}

Open in new window


Why does this work and not my original code?
0
petewinterAuthor Commented:
Does NULL still in a MySQL field still count as empty?

My Both not empty is the same as yours Julian?
0
Julian HansenCommented:
Empty is defined as

FALSE
NULL
'' /* THE EMPTY STRING */

My Both not empty is the same as yours Julian?
I took your code and added some dummy data before it - to demonstrate that the code is fine - you need to be looking at your data.

$bothempty = $row_rs_customer_selected['bespoke_pricelist'] . $row_rs_customer_selected['ink_bespoke_pricelist'];

Open in new window


Is not the same thing.

Consider this
A is empty
B is empty
Then A.B will be empty

A is empty
B is not empty
Then A.B will NOT BE empty

A is not empty
B is not empty
Then A.B will NOT BE empty

You get the same result for the second and third case - which is not correct - you only want a result when both A and B are not empty (third case) - so the concatenation test is flawed.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Does NULL still in a MySQL field still count as empty?
I don't know - I've never used NULLs in MySQL because I found them easy to confuse with NULL in PHP.  You might want to test that.  If you define your MySQL columns with NOT NULL DEFAULT '' (where '' is a pair of adjacent apostrophes) you will get a column definition that will give you PHP TRUE from PHP empty().

PHP is a loosely typed language.  Sometimes this is a good thing, but you have to know what you're doing when you make loosely typed comparisons.  Here are the links you need to understand.
http://php.net/manual/en/types.comparisons.php

PHP will use type coercion to make variables "fit" into comparisons.  Here is how that happens.
http://php.net/manual/en/language.types.type-juggling.php
0
petewinterAuthor Commented:
Thanks for your help and advice.
0
Ray PaseurCommented:
Here's an example showing that MySQL null is returned as a PHP null when you use object-oriented notation.  You can install this on your server, add your DB credentials and run it to see it in action.

Update: I tested it with array notation, too.  It also returned an empty() - compatible value.  Using MySQL at current level and PHP 5.4.45.
<?php // demo/temp_petewinter.php

/**
 * Demonstrate some of the basics of MySQLi
 *
 * References that must be understood to use PHP and MySQL(i)
 *
 * http://php.net/manual/en/mysqli.overview.php
 * http://php.net/manual/en/class.mysqli.php
 * http://php.net/manual/en/class.mysqli-stmt.php
 * http://php.net/manual/en/class.mysqli-result.php
 * http://php.net/manual/en/class.mysqli-warning.php
 * http://php.net/manual/en/class.mysqli-sql-exception.php <-- DID NOT WORK PHP 5.3+, MySQL 5.1+
 * http://php.net/manual/en/mysqli.construct.php
 * http://php.net/manual/en/mysqli.real-escape-string.php
 * http://php.net/manual/en/mysqli.query.php
 * http://php.net/manual/en/mysqli.errno.php
 * http://php.net/manual/en/mysqli.error.php
 * http://php.net/manual/en/mysqli.insert-id.php
 * http://php.net/manual/en/mysqli-result.num-rows.php
 * http://php.net/manual/en/mysqli-result.fetch-array.php
 * http://php.net/manual/en/mysqli-result.fetch-object.php
 */

// RAISE THE ERROR REPORTING LEVEL TO THE HIGHEST POSSIBLE SETTING
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';


// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

// ACTIVATE THIS TO SHOW WHAT THE DB CONNECTION OBJECT LOOKS LIKE
// var_dump($mysqli);


// CREATING A TABLE FOR OUR TEST DATA
$sql
=
"
CREATE TEMPORARY TABLE my_table
( id    INT         NOT NULL AUTO_INCREMENT PRIMARY KEY
, fname VARCHAR(24) NOT NULL DEFAULT ''
, lname VARCHAR(24) DEFAULT NULL
, xwhen TIMESTAMP   NOT NULL
)
"
;

// IF mysqli::query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}

// SHOW THE RESULTS OF THE QUERY
var_dump($res);


// LOADING OUR DATA INTO THE TABLE
$safe_fn  = $mysqli->real_escape_string('Ray');
$safe_ln  = $mysqli->real_escape_string('Paseur');

// CONSTRUCT THE QUERY USING THE ESCAPED VARIABLES
$sql = "INSERT INTO my_table ( fname, lname ) VALUES ( '$safe_fn', '$safe_ln' )";

// RUN THE QUERY TO INSERT THE ROW
$res = $mysqli->query($sql);

// IF mysqli::query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}


// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED
$id  = $mysqli->insert_id;
echo "MySQLI INSERTED A ROW CONTAINING <b>$safe_fn $safe_ln</b> WITH AUTO_INCREMENT ID = $id" . PHP_EOL;

// INSERT THAT WILL CREATE A NULL IN 'lname' COLUMN
$sql = "INSERT INTO my_table ( fname ) VALUES ( '$safe_fn' )";

// RUN THE QUERY TO INSERT THE ROW
$res = $mysqli->query($sql);

// IF mysqli::query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}


// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED
$id  = $mysqli->insert_id;
echo "MySQLI INSERTED A ROW CONTAINING <b>$safe_fn</b> WITH AUTO_INCREMENT ID = $id" . PHP_EOL;


// CONSTRUCT A SELECT QUERY
$sql = "SELECT * FROM my_table ORDER BY id";
$res = $mysqli->query($sql);

// IF mysqli_query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESULT OBJECT IN $res
// AND SO WE CAN NOW USE OTHER MYSQLI::RESULT PROPERTIES AND METHODS


// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num     = $res->num_rows;
$num_fmt = number_format($num);
if (!$num)
{
    echo PHP_EOL . "QUERY: $sql ";
    echo PHP_EOL . "FOUND NO DATA ";
}
else
{
    echo PHP_EOL . "QUERY: $sql ";
    echo PHP_EOL . "FOUND $num_fmt ROWS OF DATA ";
}
echo PHP_EOL;


// ITERATE OVER THE RESULTS SET AS AN OBJECT TO SHOW WHAT WE FOUND
echo PHP_EOL . 'USING MySQLi_Result::Fetch_<i>Object</i>(): ';
echo PHP_EOL;
while ($row = $res->fetch_object())
{
    // ROW BY ROW PROCESSING IS DONE HERE
    var_dump($row);
    echo PHP_EOL;
}
echo PHP_EOL;

// TEST THE 'lname' COLUMN FOR empty()
if (empty($row->lname)) echo PHP_EOL . 'EMPTY';

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.