Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Unsubscribed User
26,486 PointsValidate values of variables
To validate given values of variables I could also use the funtion empty($var) instead of $var=="" ?? Is it more safe or it does not matter? Thanks.
3 Answers
Chris Shaw
26,676 PointsHi Christian,
Let's start with a quick quote.
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals
FALSE.empty()does not generate a warning if the variable does not exist.
How does this differ from a manual check
Well the empty function does two things;
- First it ensures the variable exists in the current scope
- second it runs the same code we normally would which is
$var === ''
Isn't that the same?
Yes, but one thing that gets very cumbersome is writing an isset statement then a check to determine if that variable is empty whereas empty kills two birds with one stone for the lack of a better expression.
One more thing to consider
White-space, by default the empty function doesn't check for this however we can use PHP's trim function but of course this require we pass a valid variable to it first thus elimination the need for the empty function once again.
In the end it comes down to how you need your code to function and what result you need.
Unsubscribed User
26,486 PointsSo when we get a simple post request (name, email, message) we first check with empty() if there is a variable and if there is a given value. After this we should check whitspaces with trim()?
Chris Shaw
26,676 PointsI think you're confusing the explanation and the result, I'll throw an example of there.
<?php
/*
$_POST = array(
'name' => 'Chris',
'email' => 'example@example.com',
'message' => ' '
);
*/
function isPostValueValid($key) {
if (isset($_POST[$key]) && trim($_POST[$key]) !== '') {
return $_POST[$key];
}
return false;
}
if (!isPostValueValid('message')) {
// This would trigger since 'message' doesn't have a valid value
}
In this example we're doing 4 things.
- We check to see if the
$keyvalue passed to the function exists within the$_POSTarray - then we use
trimto remove any white-space from the left and right side of the string - we evaluate the result of trim using a simple equality statement
- finally we return the value of
$_POST[$key]which if valid results intrueotherwise we returnfalse
Why not use empty()?
In this case we are uncertain whether or not the key exists within $_POST therefore we use the isset function to prevent errors then trim the white-space which if the value has nothing in it should result in an empty string whereas the empty function itself won't false as I mentioned earlier because it doesn't remove white-space and we would also get an error since trim expects a valid variable.
Hope that clears it up.
Unsubscribed User
26,486 PointsIt does. Thanks a lot.