Put IF condition inside a variable - php

Is there any way to put conditions within a variable and then use that variable in an if statement? See the example below:
$value1 = 10;
$value2 = 10;
$value_condition = '($value1 == $value2)';
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
I understand this may be a bizarre question. I am learning the basics of PHP.

No need to use strings. Use it directly this way:
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
Or to evaluate, you can use this way using ", as it expands and evaluates variables inside { ... }.
I reckon it might work! Also, using eval() is evil! So make sure you use it in right place, where you are sure that there cannot be any other input to the eval() function!

Depending on what you are trying to do, an anonymous function could help here.
$value1 = 10;
$value2 = 10;
$equals = function($a, $b) {
return $a == $b;
};
if ($equals($value1, $value2)) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}
However, I would only do it like this (and not with a regular function), when you make use of use ().

== operator evaluates as a boolean so you can do
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}

Just assign result of comparision to variable.
$value1 = 10;
$value2 = 10;
$value_condition = ($value1 == $value2);
if ($value_condition) {
echo 'It works!';
} else {
echo 'It doesnt work.';
}

An if statement tests a boolean value. You could have something like this:
if (true) {
You can assign boolean values to a variable:
$boolValue = true;
You can use variables in your if statement:
if ($boolValue) {
// true
In your example:
$value_condition = $value1 == $value2; // $value_condition is now true or false
if ($value_condition) {

Related

If statement inside setted function

How to edit already setted function inside () if I want inside that function if statement
<?php
$a = 1;
function writeMsg($x) {
echo $x;
}
writeMsg(
Hello,
if ($a == 1) {
echo "men";
} else {
echo "women";
}
);
?>
I think you want to prepare the message first then write message.
<?php
$a = 1;
function writeMsg($x) {
echo $x;
}
$message = 'Hello, '. ( $a == 1 ? 'men' : 'women');
writeMsg($message);

How to check values inside foreach loop are same or not

i have a foreach loop as
foreach ($age as $ages) {
echo $ages;
}
when run this code the result is
3
3
or
1
3
i want to check if values are same or not. For example
if (values are same) {
do something...
}
else {
do something...
}
i really couldn't find how to check values are same or not.
If you are checking to see if all values are identical just use array_unique() to remove duplicate values. Then check to see if the size of the remaining array is equal to 1:
$is_all_duplicates = count(array_unique($array)) === 1;
If any duplicates will trigger this condition, just use array_unique() to remove duplicate values and then compare the sizes of the two arrays. If they are not the same you had duplicate values:
$is_some_duplicates = count(array_unique($array)) < count($array);
If values are the same, then array_count_values will have one element only:
$a = [1,2,3,3];
$acv = array_count_values($a);
if (count($acv) == 1) {
echo 'values are the same';
} else {
echo 'values are NOT the same';
}
$a = [3,3,3];
$acv = array_count_values($a);
if (count($acv) == 1) {
echo 'values are the same';
} else {
echo 'values are NOT the same';
}
The fiddle.
But the most optimal solution is a simple loop with break on first value that is not equal to first of the array:
$a = [1,2,3,3];
$hasSameValues = true;
$firstElem = array_slice($a, 0, 1)[0];
foreach ($a as $el) {
if ($el != $firstElem) {
$hasSameValues = false;
break;
}
}
if ($hasSameValues) {
echo 'values are the same';
} else {
echo 'values are NOT the same';
}
echo PHP_EOL;
$a = [3,3,3];
$hasSameValues = true;
$firstElem = array_slice($a, 0, 1)[0];
foreach ($a as $el) {
if ($el != $firstElem) {
$hasSameValues = false;
break;
}
}
if ($hasSameValues) {
echo 'values are the same';
} else {
echo 'values are NOT the same';
}
Yet another fiddle.
You possible want to 'say' :)
foreach ($ages as $age) {
echo $age;
}
I understand that you want to check if only previous value is equal with actual value.
To check that write this:
$ages = [1,1,7,2,2,4,1,2];
foreach ($ages as $age) {
if(isset($temp) ? !($temp == $age) : true){
echo $age;
}
$temp = $age;
}
The result is: 172412
The code:
Check if variable $temp is set: isset($temp)
If it is set, check if it is NOT equal with last age value: !($temp == $age)
If the $temp isn't set, set true to run the code from if statement (echo the age)(is there for first run).

checking zero is null or not in php

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!

PHP output only one value in for each

I'm having trouble with output in for each function in PHP (actually don't know how to set the code to do what I need). I'd like to output some text if every item in foreach is equal to some value. If I put
foreach($items as $item) {
if($item == 0){ echo "true"; }
}
I will get true for every item and I need to output true only if all items are equal to some value.
Thanks!
This is most likely due to PHP type juggling your values. Your values are probably not numeric so when you do a loose comparison (==) PHP converts them to integers. Strings that do not start with digits will become zero and your statement will be true.
To fix this use the === comparison operator. This will compare value and type. So unless a value is the integer zero it will be false.
if($item === 0){ echo "true"; }
If you are trying to see if all items are equal to some value this code will do this for you:
$equals = 0;
$filtered = array_filter($items, function ($var) use ($equals) {
return $var === $equals;
});
if (count(count($items) === count($filtered)) {
echo "true";
}
This peice of code work for most type of variables. See how it works in inline comment.
$count=0; // variable to count the matched words
foreach ($items as $item)
{
if($item == $somevalue)
{
$count++; // if any item match, count is plus by 1
}
}
if($count == count($items))
{
echo "true"; // if numbers of matched words are equal to the number of items
}
else
{
echo "false";
}
Hope it works , And sorry for any mistake
$ok = true;
foreach ($items as $item)
{
if ($item != 0)
{
$ok = false;
}
}
if ( $ok == true)
{
echo 'true';
}
$bool = 0;
foreach($items as $item) {
if($item == $unwantedValue)
{ $bool=1; break; }
}
if($bool==0)
echo 'true';
$equals=true;
foreach($items as $item) {
if($item!=0)
{
$equals=false;
break;
}
}
if($equals) {
echo 'true';
}
if you want to check the values is same with some value in variabel and print it use this
<?php
$target_check = 7;
$items = array(1, 4, 7, 10, 11);
foreach ($items as $key => $value) {
if ($value == 7) echo "the value you want is exist in index array of " . $key . '. <br> you can print this value use <br><br> echo $items[' . $key . '];';
}
?>
but if you just want to check the value is exist in array you can use in_array function.
<?php
$target_check = 2;
if (in_array($target_check, $items)) echo "value " . $target_check . 'found in $items';
else echo 'sorry... ' . $target_check . ' is not a part of of $items.';
?>
<?
$items=array(0,1,2,0,3,4);
foreach($items as $item) {
if($item == 0){ echo "true"; }
}
?>
your code work!
check the source of the $items array

PHP global variables not working inside a function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php function variable scope
I am using below code to test with a global variable. It seems that a global variable cannot be compared inside a function.
Why is it not displaying 'hello world' in the output?
Below is the code that I am trying:
<?php
$bool = 1;
function boo() {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
When I remove function boo(), 'hello world' is displayed. Why is it not displaying when function exists?
use global $var to access your variable
<?php
$bool = 1;
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
boo();
?>
Or a safer way using pointers would be to
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
Looks like homework, still:
<?php
$bool = 1;
boo();
function boo() {
global $bool;
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
Or
<?php
$bool = 1;
boo(&$bool);
function boo(&$bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
?>
Call you function, and pass $bool as a parameter and return the value.
$bool = 1;
$bool = boo($bool);
function boo($bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
return $bool;
}
use this way
$bool = 1;
function boo($bool) {
if ($bool == 1) {
$bool = 2;
echo 'Hello World';
}
}
boo($bool);

Categories