how to detect parameter is not empty on laravel? - laravel

how to detect parameter is not empty on laravel?
example i have params body like below :
{
email : "haha#example.com"
}
when params email is exist's i want echo some string, example : "parameter email is exist's"
if params email not exits just echo "parameter is not exist's"
if(params['email'] != null) {
print params email is exists's
}else{
print params email doesn't exists's
}
how do that, i'm new on php / laravel?

Try Below Code
if( isset(params['email']) && !empty(params['email']) ) {
echo " params email exists"
}else{
echo "params email doesn't exist"
}

Object oriented Laravel way is:
if($email = array_get($params, 'email')){
echo " params email exists";
}else{
echo "params email doesn't exist";
}

Use php build in empty() function which checks if array key exists, is not null and is not empty according to data type.
if (!empty($params['email']) {
// not empty
}
From manual:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

If you want to know if the array key exists use
if(array_key_exists('email',$params)){
//true
}
to check if the key exists and its not empty use
if(array_key_exists('email',$params)&&!empty($params['email']){
//true
}
see Robert's answer for what is seen as empty by the empty function.

Related

DXL in DOORs to check specific outlink and return the number of occurrences for a certain word

Thanks for any help.
I am currently trying to get a DXL script to return all occurrences for a certain word in the module in the outlink. The outlink "test" will need to be a wild card, if possible, to find all other outlnks that have the word "test" unsure on if that's possible. The attribute in the outlink will be "Compliance". Maybe need a while loop?
Link outlnk
string tmn = ""
string Status = ""
int Count = 0
for outlnk in obj-> "*" do
{
tmn = fullName source(outlnk)
Object tgt = source(outlnk)
if(matches("Test", tmn))
{
Status = tgt."Compliance"
if (Status == "Compliant"){
Count++
}
}
}
display Count""
I tried to get the above code to work with the below while loop. But can not figure out the correct syntax.
Regexp Check = regexp2 "Compliant"
while (!null Status && Check Status)
{ Status = Status[end 0 + 1:]
Count++
}

Puppet test defined variable with an empty string

I'm trying to test if a variable has a value with a simple if in puppet, but it never reachs the else condition that I'm trying to achieve.
ex :
if $::some_var['some:name'] != undef {
$var = $::some_var['some:name']
}
else {
$var = $::some_var['another:name']
}
some_var cames from a json file, and if the ::some_var['some:name'] is emtpy it will put $var as empty, but what I'm trying to achieve is, if the var is defined but is empty to actually go through the else, can this be achieved using for instance
if defined($::some_var['some:name']) {
$var = $::some_var['some:name']
}
else {
$var = $::some_var['another:name']
}
I resolved it by using
$var = ''
if $var =~ String[1] {
notify{"The value is: ${var}": }
}
else {
notify{"The value is: empty": }
}
file {'/tmp/helloworld.txt': ensure => present, content => "Hello World!", }
If I define $var whit some value, I'll met the if condition, if $var is empty I'll met the else.

Why does an error occur when I try to test if a member variable (string) in a class is empty?

I'm working on a project and need to test one of my class' member variables to verify that the user did indeed enter a string.
I've also tried using (patronName == '') and (patronName == "") but have had no luck.
Edit: Using "\n" fixes the error but the program ends without allowing the user to enter a name.
std::string Restaurant::getPatronName()
{
bool controlFlag = true;
do
{
getline(std::cin,patronName);
if ((std::cin.fail()) || (patronName == '\n'))
{
std::cout << "You must enter a name!" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
controlFlag = false;
}
} while (controlFlag);
return patronName;
}
The function should read and store the name entered by the user into patronName. When trying to build, I get an error that says "no match for 'operator=='". Could this be because the object called in main is a pointer of type Restaurant?
Besides the type mismatch between the character '\n' and the std::string patronName, we can find at https://en.cppreference.com/w/cpp/string/basic_string/getline that std::getline(input, str, delim);
Extracts characters from input and appends them to str until […] the next available input character is delim, […], in which case the delimiter character is extracted from input, but is not appended to str.
So there won't be any '\n' character, if delim is the newline, in the first place.
You can use std::basic_string::empty() to check if a string is empty.
What happens with '\n' is you are comparing a string with a char, which, i suspect, there is no operator== defined for this case. If you are sure the string isn't empty, you can call operator[] formerName[0], which returns a char.
You have to write patronName == "\n" because you cannot compare string and character

C++: ambiguous overload for 'operator='

I'm using the Code::Blocks IDE and whenever I try to build the file this code brings up an error:
{
cin >> input ;
locale loc;
for (string::size_type i=0; i<input.length(); ++i)
input[i] = tolower(input[i],loc);}
{
if (input == "not")
"" != "";
else if (input == "and")
"" && "";
else if (input == "or")
"" || "";
else if (input == "yes")
input = true;
else if (input == "no")
input = false;
}
The error occurs where I try to make the word "no" equal to the Boolean operator false.
This brings up this error:
Projects\Samantha\main.cpp|40|error: ambiguous overload for 'operator=' (operand types are 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' and 'bool')|
Now I've tried searching for this problem but I haven't been able to find anything to help me. If someone could please help me to figure what the problem is and how I can fix it I would be very appreciative.
The problem is that you are trying to assign a Boolean to a string.
You have two options here
Create a bool variable where you will store your false value
assign a string literal instead of a boolean input = "false";
Please note that the first three ifs are doing nothing since you are performing logical operations on empty string literals and not storing the result anywhere.
I'd also recommend to avoid using if() conditions without following braces since that is an error-prone, difficult to maintain and difficult to read approach.
else if (input == "yes")
{
booleanVariable = true;
}

I just want to know that why "message.time" is always returning the value 0

Before calling below function and have already outputted the msg1 which I can see in Trace Window.
tmdiff = timenow() - msg1.time;
tmdiff is always returning the value of timenow() and the value of msg1.time is always Zero.
You should also catch your own mesage in the on message routine.
So assumming that msg1 is global to the following.
on message *
{
if ( msg1.id == this.id )
msg1=this;
}

Resources