i am working on validation and comparisons!! i have a field that can contain the value $val=0 or $val="some-value" or $val="" or $val=0 basically i want the $val="0"or $val=0 to be validated as true..
if($val){
//works for $val="some-value"
//doesnot work for $val=0 or $val="0";
} else
{
//works corrent for $val=""
}
one conditional approach i used is
$val="";
if($val || $val==0){
echo "true";
}
else
{
//should be false but it is true
echo "false";
}
did you try this?
$val = "";
if ($val == '0') {
echo "TRUE";
# code...
}
elseif ($val == "") {
echo "FALSE";
}
There is a useful php native function is_null
if (is_null($val) || $val === "") {
//invalid
} else {
//valid
}
You can use PHP integer casting & can do it like this:
if ((int) $val === 0) {
return true;
} else {
return false;
}
Hope this helps!
Related
how I can define a variable inside a PHP function and make it reachable from another function? I want to set $error variable to "1".
This how would the code look like:
function checkthename ($name){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
function phone ($phone){
if ($name == "TESTTEST"){
echo $error = 0;
}
else {
echo $error = 1;
}
}
etc.
And in end of the code there would be an another function which is checking if there is any $error variable set to 1
function checktheerror($error){
if ($error == 0){
echo "no error in this code";
}
elseif ($error == 1){
echo "there is one or multiple errors";
}
}
Thank you so much for your help!
So, I want to check the users-input, if it contains some of these characters:
" ' < >
I hope someone can show me a better way with less code
Thanks!
I used preg_match, but i just managed it with 4 nested if's.
/*Checks if the given value is valid*/
private function checkValidInput($input)
{
/*If there is no " */
if(preg_match('/"/', $input) == false)
{
/*If there is no ' */
if(preg_match("/'/", $input) == false)
{
/*If there is no <*/
if(preg_match("/</", $input) == false)
{
/*If there is no >*/
if(preg_match("/>/", $input) == false)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
You could create a regex class
preg_match('#["\'<>]#', $input);
Edit:
If you need to check for all characters then use strpos() with for loop
function checkInput($val) {
$contains = true;
$required = "<>a";
for($i = 0, $count = strlen($required); $i < $count ; ++$i) {
$contains = $contains && false !== strpos($val, $required[$i]);
}
return $contains;
}
var_dump(checkInput('abcd<>a')); // true
var_dump(checkInput('abcd>a')); // false, doesn't contain <
I have this piece of code:
if($this->ask('Is this holiday booked? [y|N]')) {
$holiday->booked = true;
} else {
$holiday->booked = false;
}
in a Laravel 5.2 command, but whatever the response it always seems to return true.
I also tried:
if($this->ask('Is this holiday booked? [y|N]') === true) {
$holiday->booked = true;
} else {
$holiday->booked = false;
}
But this always enters it into the database as false regardless of if I enter y or n.
It will no doubt be something stupid, but can anyone see where I'm going wrong?
Thanks.
Ended up using:
if(!$this->confirm('Is this holiday booked? [y|N]'), false) {
$holiday->booked = false;
} else {
$holiday->booked = true;
}
Or Try This :) :
$input = $this->ask('Is this holiday booked? [y|n]');
if($input == 'y' || $input == 'Y') {
$holiday->booked = true;
}
elseif($input == 'n' || $input == 'N') {
$holiday->booked = false;
}
else {
$this->error("wrong input");
}
I want to check if $AD = 1, then if not pass to another function, but if $AD = 1 check if AC = contains some value, if not do some stuff.
if ($AD == '1'){
if ($AC == ''){
echo 'AC is empty';
} else {
///////Function/////
}
}
Thanks.
An else if statement must always correspond to a beginning if statement on the same "nesting level".
Your inner else if does not have any beginning if statement and it therefore fails.
if ($AD == 1) {
if ($AC == '') {
echo 'empty';
}
}
else {
otherFunction($AD);
}
Since this is basic PHP (actually IFs are mostly not language-dependent) stuff, I recommend you reading a beginner's tutorial.
if ($AD == 1){
if ($AC == ''){
// some code where ac has the value
} else {
// some code where ac doesn't have the value
}
} else {
// other function
}
That should do it
In a PHP project I am working on right now, I have some code similar to this:
$allVarsTrue = TRUE;
if ($foo && $bar) {
for ($x=1;$x<=5;$x++) {
if (!somerandomtest($x)) {
$allVarsTrue = FALSE; // if $x fails the test, $allVarsTrue is set to false
}
}
} else { // if either $foo and $bar is false, $allVarsTrue is set to false
$allVarsTrue = FALSE;
}
if ($allVarsTrue) {
echo "True";
} else {
echo "False";
}
I would like to write this more succinctly, something like this
// This code does not work.
if ($foo &&
$bar &&
for ($x=1;$x<=5;$x++) {
somerandomtest($x);
}) {
echo "True";
} else {
echo "False";
}
How can I rewrite the existing code more succinctly?
One option is to move your loop into its own function:
function performTests() {
for(…) { if(!test(…)) return FALSE; } # return early, no need to iterate over remaining items
return TRUE;
}
if($foo && $bar && performTests()) {
…
} else {
…
}
Wrap it in a function:
function testStuff($foo, $bar){
if (!$foo || !$bar) {
return FALSE;
}
for ($x=1;$x<=5;$x++) {
if (!somerandomtest($x)) {
return FALSE;
}
}
return TRUE;
}
And then:
if (testStuff($foo, $bar)) {
echo "True";
} else {
echo "False";
}
You can't really. However, you can break the for loop as soon as first test is failed
if ($foo && $bar) {
for ($x=1;$x<=5;$x++) {
if (!somerandomtest($x)) {
$allVarsTrue = FALSE; // if $x fails the test, $allVarsTrue is set to false
break; //no point in firther iterating
}
}
} else { // if either $foo and $bar is false, $allVarsTrue is set to false
$allVarsTrue = FALSE;
}