Wrong output in php program - php

Following is my code here actually o/p should be hi..but it is giving no
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='xyz,ccc';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
output:hi
actual result should be 'no'.

Side note, this is bad code:
in_array(isset($weekendArr) && $weekendArr,$arr)
do it like
isset($weekendArr) && in_array($weekendArr,$arr)
and in_array is not strict so this
in_array(true,array('w','s'))
will be allways TRUE
do it with:
in_array(true,array('w','s'),true)
and you see.
And you can't check an array with an array the $needle be an STRING here.
The only solution is to do splitt your STRING into two values and then check two times for TRUE
$c='Sunday,Monday';
foreach(explode(',',$c) as $check){
if(in_array($c,$arr,true))
{
echo $check.' is in array';
}
else
{
echo $check.' is NOT in array';
}
}
Hope that helps a little.

<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$day='Sunday'; //You cant test both days ! Just one value at a time
if(true === in_array($day, $listDays))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
Or option two if you want to test different days
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$dayToTest='Sunday, Monday'; //Here we have multiple days
$tabTest = preg_split(',', $day); //split into an array
//Then test for each string in tabTest
foreach($tabTest as $string)
{
if(true === in_array($string, $listDays))
{
echo $string.' is OK';
}
else
{
echo 'no';
}
}
?>

Change your code to:
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='Sunday,Monday';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}

Related

String same multiple different number (PHP)

I need help.
As I do?
String same other string with different number.
Example
if ($current_server == "Lobby-01") {
echo "visiting in Lobby"
} elif ($current_server == "Lobby-02"){
echo "visiting in Lobby"
} and more..
I thought about trying
if ($current_server == "Lobby-/[0-99]+/"){
echo "visiting in Lobby"
} else {
//is false, then it will not show the message
}
is correct?
How do I do it?
Can you help me?
Use preg_match:
if (preg_match("/\bLobby-[0-9]{2}\b/i", $current_server)) {
echo "visiting in Lobby";
}
else {
// no message
}
Demo

php If statement inside html table wrong output

i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table

How to check if all rows match a criteria in PHP

I want to check if a certain field of all rows match a criteria.
So if all rows has in the Status field 'RUN' as value then echo "Success".
If there is one row with END as value then echo "Fail":
I'm guessing I need a loop and an IF statement ?
I was thinking something like this but it doesnt return anything:
while($source_row = mysqli_fetch_array($source_selection)){
if ( ($source_row['Stat']) == ("Run" ) {
echo "Success<br />";
} else
echo "Fail";
}
I don't want to echo each row, I want all rows to match a criteria then echo, if one doesn't match a criteria then echo as well.
This should work for you:
First of all you have to fetch all rows with mysqli_fetch_all(). After this I extract only the Stat column with array_column(). Then I just simply array_fill() an array with X values as you have in $states with the value "Run". And check if every value is equals.
$result = mysqli_fetch_all($source_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else {
echo "Fail";
}
There are some error in the code and you can use the count to match -
$count = 0;
while($source_row = mysqli_fetch_array($source_selection)){
if ($source_row['Stat'] == "Run" ) {
$count++;
}
}
if($count != mysqli_num_rows($source_selection)) {
echo "Fail";
} else echo "Success";
For best performance you can do it directy on the SQL:
select count(*) from table where Stat <> 'Run';
And then test the returning value to check that is greater than zero.
To do it with php you should know that when you find an error you can stop the iterations. The code would look like that:
while($source_row = mysqli_fetch_array($source_selection)){
if ( $source_row['Stat'] != "Run" ){
$fail = true;
break;
}
}
if ($fail) {
echo "Fail";
} else
echo "Success";
}
Try this
$success=true;
while($source_row = mysqli_fetch_array($source_selection)){
if ( ($source_row['Stat']) != ("Run" ) {
$success=false;
}
}
if ($success) {
echo "Success";
} else {
echo "Fail";
}

how can i do first variable like second variable in php

my code is
<?php if($_SERVER['REQUEST_URI'] == "/scripts/script1.php")
{
echo 'yes';
}
else
{
echo 'no';
}
some time my URL come like
www.example.com/scripts/script1.php?var1=value1&var2=value2
How can do that with PHP
URL like function....
Make use of strpos in PHP
<?php
if(strpos($_SERVER['REQUEST_URI'],"/scripts/script1.php")!==false)
{
echo 'yes';
}
else
{
echo 'no';
}
A slight variation on Shankar's answer
$result = "no";
if( strpos( $_SERVER[ "REQUEST_URI" ], "/scripts/script1.php" ) !== false ) {
$result = "yes";
}
By setting the default value for the variable, you can make your code more concise and improve readability.

How do I clean up this if/else statement? (refactoring)

I know there must be a nicer way to do this, but whenever I search "&&" i don't get good enough results...
<?php
if (empty($row[ContactName]) && empty($row[ContactEmail]) && empty($row[ContactPhone]) && empty($row[Website])){
echo "Not Provided";
}
else{
...do stuff...
}
?>
Thanks!
What is wrong with the original code?
<?php
if (empty($row[ContactName])
&& empty($row[ContactEmail])
&& empty($row[ContactPhone])
&& empty($row[Website]))
{
echo "Not Provided";
}
else{
...do stuff...
}
?>
Looks like fine code to me...
<?php
$i=1;
$ar=array('ContactName','ContactEmail','ContactPhone','Website')
foreach($ar as $a)
if (empty($row[$a]))
{
$i=0;
break; //to make code fast
}
if($i) //do stuff
else echo 'not provided';
?>
or if you really want to make your code extra small then change your column name in database
From To
ContactName Col1
ContactEmail Col2
ContactPhone Col3
Website Col4
and then do
<?php
$i=1;
for($a=1;$a<5;$a++)
if (empty($row['Col'.$a]))
{
$i=0;
break;
}
if($i)//do stuff
else echo 'Not Provided';
?>
However it is not good to rename column as it will make your db somewhat less understandable.
As php functions have a lot of inconsistency, It's always a good idea using a library to make it more consistent.
put this function in that library:
function empty()
{
foreach(func_get_args() as $arg) if (empty($arg)) return false;
return true;
}
usage:
if (empty($row[ContactName], $row[ContactEmail], $row[ContactPhone], $row[Website])) {
echo "Not Provided";
}
you could make it as short as this if compactness was more important than readability :)
$required = array('ContactName', 'ContactEmail', 'ContactPhone', 'Website');
$ra = array_filter(array_intersect_key($row,array_flip($required)));
if(empty($ra)){
echo "Not Provided";
}
else{
//....
}
you can't put array_filter inside empty() or you get "Fatal error: Can't use function return value in write context"
1) array flip is turning your $required values into keys
2) array_intersect_key throws away any $row keys not found in $required
3) array_filter throws away any empty values
leaving you with a zero length array, which is empty. No need to worry about array lengths or loops
<?php
$flag = 1;
if (empty($row[ContactName])){
$flag = 0;
}
if (empty($row[ContactEmail])){
$flag = 0;
}
if (empty($row[ContactPhone])){
$flag = 0;
}
if (empty($row[Website])){
$flag = 0;
}
if($flag == 0){
echo "Not Provided";
}else{
//do stuff
}
?>
OR
$flag = 1;
$i=0;
$mustarray = array('ContactName','ContactName','ContactName','ContactName'); //or any number of fields that you want as mandatory
foreach($yourresult as $row){
if(empty($row[$mustarray [$i]])){
$flag = 0;
}
$i++;
}
if($flag == 0){
echo "Not Provided";
}else{
//do stuff
}
Do not play with making arrays and stuff just to make code look nicer.
As Tommy pointed
<?php
if (empty($row[ContactName])
&& empty($row[ContactEmail])
&& empty($row[ContactPhone])
&& empty($row[Website]))
{
echo "Not Provided";
}
else{
...do stuff...
}
?>
this code is nice, just need proper formatting like he pointed.
Making arrays and then launching loop will decrease performance
<?php
$i=1;
for($a=1;$a<5;$a++)
if (empty($row['Col'.$a]))
{
$i=0;
break;
}
if($i)//do stuff
?>
this might look small, nice and "pro", but has more operations to give same results as a simple if instruction.
Not saying that it cant be done faster, i don't know php well, just remember about code executing speed.

Categories