How to check if $_GET is empty?
You said it yourself, check that it's empty
:
if (empty($_GET)) {
// no data passed by get
}
See, PHP is so straightforward. You may simply write, what you think ;)
This method is quite secure. !$_GET
could give you an undefined variable E_NOTICE if $_GET
was unset (not probable, but possible).
empty($_GET['variable']) blocks the script when the variable has no value
A blank page is a classic example of a PHP error. You need to set up and use PHP error logging facility like so:
error_reporting(E_ALL);
ini_set('display_errors', 1);
At the very top of your page.
Rewriting your page I would do this:
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (!isset($_GET['a']) || is_null($_GET['a'])) {
header("Location: https://google.com");
exit();
}
echo "hello";
How do I check for an empty/undefined/null string in JavaScript?
Empty string, undefined, null, ...
To check for a truthy value:
if (strValue) {
// strValue was non-empty string, true, 42, Infinity, [], ...
}
To check for a falsy value:
if (!strValue) {
// strValue was empty string, false, 0, null, undefined, ...
}
Empty string (only!)
To check for exactly an empty string, compare for strict equality against ""
using the ===
operator:
if (strValue === "") {
// strValue was empty string
}
To check for not an empty string strictly, use the !==
operator:
if (strValue !== "") {
// strValue was not an empty string
}
check if variable empty
If you want to test whether a variable is really NULL
, use the identity operator:
$user_id === NULL // FALSE == NULL is true, FALSE === NULL is false
is_null($user_id)
If you want to check whether a variable is not set:
!isset($user_id)
Or if the variable is not empty, an empty string, zero, ..:
empty($user_id)
If you want to test whether a variable is not an empty string, !
will also be sufficient:
!$user_id
Laravel check if collection is empty
You can always count the collection. For example $mentor->intern->count()
will return how many intern does a mentor have.
https://laravel.com/docs/5.2/collections#method-count
In your code you can do something like this
foreach($mentors as $mentor)
@if($mentor->intern->count() > 0)
@foreach($mentor->intern as $intern)
<tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}">
<td>{{ $intern->employee->FirstName }}</td>
<td>{{ $intern->employee->LastName }}</td>
</tr>
@endforeach
@else
Mentor don't have any intern
@endif
@endforeach
PHP - Check GET Variables Exist And Not Empty
You can just use empty which is a PHP function. It will automatically check if it exists and whether there is any data in it:
if(empty($var))
{
// This variable is either not set or has nothing in it.
}
In your case, as you want to check AGAINST it being empty you can use:
if (!empty($u) && !empty($p))
{
// You can continue...
}
Edit: Additionally the comparison !==
will check for not equal to AND of the same type. While in this case GET/POST data are strings, so the use is correct (comparing to an empty string), be careful when using this. The normal PHP comparison for not equal to is !=
.
Additional Edit: Actually, (amusingly) it is. Had you used a !=
to do the comparison, it would have worked. As the ==
and !=
operators perform a loose comparison, false == ""
returns true - hence your if statement code of ($u != "" && $p != "")
would have worked the way you expected.
<?php
$var1=false;
$var2="";
$var3=0;
echo ($var1!=$var2)? "Not Equal" : "Equal";
echo ($var1!==$var2)? "Not Equal" : "Equal";
echo ($var1!=$var3)? "Not Equal" : "Equal";
echo ($var1!==$var3)? "Not Equal" : "Equal";
print_r($var1);
print_r($var2);
?>
// Output: Equal
// Output: Not Equal
// Output: Equal
// Output: Not Equal
Final edit: Change your condition in your if statement to:
if ($u != "" && $p != "")
It will work as you expected, it won't be the best way of doing it (nor the shortest) but it will work the way you intended.
Really the Final Edit:
Consider the following:
$u = isset($_GET["u"]); // Assuming GET is set, $u == TRUE
$p = isset($_GET["p"]); // Assuming GET is not set, $p == FALSE
Strict Comparisons:
if ($u !== "")
// (TRUE !== "" - is not met. Strict Comparison used - As expected)
if ($p !== "")
// (FALSE !== "" - is not met. Strict Comparison used - Not as expected)
While the Loose Comparisons:
if ($u != "")
// (TRUE != "" - is not met. Loose Comparison used - As expected)
if ($p != "")
// (FALSE != "" - is met. Loose Comparison used)
how to check if all the variable not empty in PHP
Like this:
$array = [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '',
];
if(count($array) != count(array_filter($array))){
echo 'some rows are empty';
}
If you want to know which specific fields are empty you can use array_diff_key
$array = [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '',
];
$filled = array_filter($array);
if(count($array) != count($filled)){
$empty = array_diff_key($array, $filled);
echo 'field(s) '.implode(', ', array_keys($empty)).' are empty';
}
Ouputs:
field(s) email are empty
Obviously you can improve on the error string, or what have you. So in your case I would do something like this
if(count($array) != count($filled)){
$empty = array_keys(array_diff_key($array, $filled));
$last = '';
if(count($empty) > 1 ){
$last = ' and '.array_pop($empty);
}
$fields = implode(', ', $empty).$last;
echo json_encode(array('condition' => 'error','data' => 'Please fill in '.$fields));
}
So it should output something like this:
Please fill in email
Please fill in first_name and email
Please fill in first_name, last_name and email
You could even do:
$empty = array_map(function($item){
return ucwords(str_replace('_', ' ', $item));
}, $empty);
Which would take, first_name
and change it to First Name
if you want to get fancy.
So... Putting all that togather:
$array = [
'first_name' => '',
'last_name' => '',
'email' => '',
];
$filled = array_filter($array);
if(count($array) != count($filled)){
$empty = array_map(function($item){
return ucwords(str_replace('_', ' ', $item));
},array_keys(array_diff_key($array, $filled)));
$last = '';
if(count($empty) > 1 ){
$last = " and '".array_pop($empty)."'";
}
$fields = "'".implode("', '", $empty)."'".$last;
echo json_encode(['condition' => 'error','data' => 'Please fill in '.$fields]);
}
Outputs:
{"condition":"error","data":"Please fill in 'First Name', 'Last Name' and 'Email'"}
You can see it here live
And now it tells them exactly what fields they need, instead of some generic error.
HTTP GET: Check if var is empty
Use empty()
if(!empty($_GET['Field110'])){
array_push($apnsarray, "E02");
}
// etc...
How do I check if a $_GET parameter exists but has no value?
Empty is correct. You want to use both is set and empty together
if(isset($_GET['app']) && !empty($_GET['app'])){
echo "App = ".$_GET['app'];
} else {
echo "App is empty";
}
Related Topics
Why Are Floating Point Numbers Printed So Differently
How to Throttle My Site's API Users
Sorting Multidim Array: Prioritize If Column Contains Substring, Then Order by a Second Column
Avoiding Form Resubmit in PHP When Pressing F5
Pre-Incrementation VS. Post-Incrementation
Creating Anonymous Objects in PHP
PHP Fatal Error: Call to Undefined Function MySQLi_Stmt_Get_Result()
How to Upload and Parse a CSV File in PHP
Symfony2 - Assetic - CSS Font Icons
Best Methods to Clean Up a Hacked Site with No Clean Version Available