Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if variable is empty or not

Tags:

php

So, I have an input as below:

<input type="text" id="rhc_phone" name="rhc_phone" placeholder="Phone number"/>     

Which in return store as a variable:

var data = {

    'phone': $('#rhc_phone').val()

         };

This then can be extracted via following:

 <?php
  echo '
    <p>Call me at ' . $_POST['phone'] . '.</p>              
     ';
 ?>

However, the input can be left out.

Now, what is the if statement to check if the variable has value and if not, then show something else like below.

 <?php
    if (!isset($_POST['phone']) = 0) {
        echo '
       <p>Call me at ' . $_POST['phone'] . '.</p>               
        ';
    }else{
       <p>No Number</p> 
    }
 ?>

Is it correct?

Thanks!

like image 747
Steve Kim Avatar asked Jan 26 '26 08:01

Steve Kim


1 Answers

Use empty

if(!empty($_POST['phone'])
{ 
   echo '<p>Call me at'. $_POST['phone'] . '</p>'; 
} else {
   echo '<p>No Number</p>';
}
like image 179
aldrin27 Avatar answered Jan 28 '26 20:01

aldrin27



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!