This might seem weird to ask such a stupid question.
However, I am not able to figure out why I am getting wrong value with such a small piece of code.
Code is:
<?php
error_reporting(0);
$result_all = 'User not found';
if($result_all['BookingDetail']['BookingReference']){
echo "<br>Output in if :- ".$result_all['BookingDetail']['BookingReference'];
}else{
echo "<br>Output in else:- ".$result_all['BookingDetail']['BookingReference'];
}
I expected output to be blank however it returns first character of the string. I know checking if its is_array and isset will resolve my problem.
But I want to understand why its happening?
Problem:-
You are assigning 'User not found' to $result_all variable in your script, which makes it a string variable.
Since $result_all is a string, any index like BookingDetail is considered as 0 for the string and that's why it will give the first character of string
Solution:-
You need to use isset() to check index exist or not?
<?php
//comment these two lines when code started working fine
error_reporting(E_ALL);
ini_set('display_errors',1);
if( isset( $result_all['BookingDetail']['BookingReference'] ) ){
echo "<br>Output in if :- ".$result_all['BookingDetail']['BookingReference'];
}else{
echo "<br>Output in else:- User not found";
}
I ran this on PHP 7.3
This is the output:
Output in if :- UIllegal string offset 'BookingReference' on line 5
Please consider upgrading your PHP version
Because you convert it in array because of the same name of the variable.
$result_all in ARRAY by doing this $result_all['BookingDetail']['BookingReference']
and not define which value of array you want to show so by default it showing the first character of string.
$result_all = 'User not found';
Output:- U
Related
I'm trying to run a function but only on specific pages. I thought this would be fine...
$allowed_pages = array('/admin/update.php', '/admin/system.php', '/admin/install.php');
if($_SERVER['SCRIPT_NAME'] == $allowed_pages) {
myCoolFuntion();
}
...but it doesn't seem to work as expected and the myCoolFuntion() runs regardless of what page I am on.
If I echo $_SERVER['SCRIPT_NAME'] on any given page, I can see it does match up correctly (it matches a page specified in the array, for example, /admin/update.php as expected) and so I know the values in the array are in the correct format.
What am I doing wrong?
Based on the example provided, I can't see any way that myCoolFunction(); can ever execute.
$_SERVER['SCRIPT_NAME'] will never be equal to $allowed_pages because the first is a string and the second is an array.
Instead of the code as presented, use a function such as in_array to verify that the SCRIPT_NAME value is in the array:
$allowed_pages = array('/admin/update.php', '/admin/system.php', '/admin/install.php');
if (in_array($_SERVER['SCRIPT_NAME'], $allowed_pages)) {
myCoolFunction();
} else {
echo 'Not found in array';
}
in_array is defined thus:
Checks if a value exists in an array
More information about in_array is in the official docs.
I have a file that use's this code
<?php echo $block->getStoreName(); ?>
to output the following on the website
First Second Third
However I only want to only output the Third element of the string above, First Second do not change they always stay the same.
Third
I'm using this code to retrieve the Third part of the string
echo explode('First Second', $block->getStoreName())[1];
Its throwing up an error.
Error filtering template: Notice: Undefined offset: 2 in
/home/xyz/m230.xyz.com/app/code/Vendor/Siteinfo/view/frontend/templates/storename.phtml
on line 1
Line 1 in storename.phtml is
<?php echo explode('First Second', $block->getStoreName())[1]; ?>
I'm unsure if thats the correct way of doing it.
UPDATE - Have attempted a clearer explanation of what I'm trying to achieve.
echo explode(' ', $block->getStoreName())[2];
This should do the trick.
// checks if string has "Unique"
if(mb_strpos($block->getStoreName(),'Unique') !== false){
// prints "Unique"
echo "Unique";
}
i have a problem like the following sample code :
$code='100'; //$maybe $code='0' or $code='1' ... i just set a number as an sample
$xx=array(
'0'=>array(a,b,c),
'1'=>array(d,e,f),
........
'100'=>array(aa,bb,cc)
);
I want find $code in array :
if($code==$xx['$code']){
echo $xx['code'][0]; //if i want get the value 'aa'
}
But it seems like $xx['$code'] doesn't work.
Do anyone know the right way to solve it?
Firstly you need to use array_key_exists for getting within if condition and then after you can use it simply like as
if(array_key_exists($code,$xx)){
echo $xx[$code][0];
}
or can simply use isset instead like as
if(isset($xx[$code])){
echo $xx[$code][0];
}
you shouldn't use single quotations here,
when filling displaying a variable either use without quotations or put it between double "" try $xx[$code] or $xx["$code"]
Use isset OR !empty to check key exist in array or not. It will also check that key has valid value.
if(isset($xx[$code])){
echo $xx[$code][0];
}
OR
if(!empty($xx[$code])){
echo $xx[$code][0];
}
if($code==$xx['$code']){
echo $xx[$code][0]; //if i want get the value 'aa'
}
If you use '$code' the content of $code will not be checked as '' interpret everything as a string and won't look a variables in it.
You can't sace 'code' either, as code is only the name of the variable you use.
I am reading file. I read all data in $part. it is working fine but issue arise when i use empty function. It should display NULL but it is not showing NULL.
My code is as follows:
echo "\nParts------------".$parts[$r]."\n";
echo "\nParts---Size---------".strlen($parts[$r])."\n";
var_dump($parts[$r]);
// $parts[$r]=trim($parts[$r],' ');
//$parts[$r]=str_replace('""','',($parts[$r]));
if(empty($parts[$r]))
{
$entryarray[$c][$c2]='NULL';
}
else if(strlen($parts[$r])<1) //removing special characters
{
$entryarray[$c][$c2]='NULL';
// array_push($entryarray[$c]);
$valueArray=$valueArray.",".'NULL';
}
when i vardump($part) then it is showing its length is 2 instead of 0.
How to display it NULL. i cant write check as if(strlen($parts<2)) cause there is data in file which has lenght less then 2.
I think you meant to use isset instead.
$foo = array("a", "b");
isset($foo[2]); // false
The empty function check if is null or empty string or 0 or something like that.
If you want to chack if is null, i recommend u to use "is_null()"
To check if a key is on array use
array_key_exists($key,$array);
Could anyone please explain to me why the following line of code prints out true?
$a = "string";
if(isset($a['error'])) echo true; else echo false;
When I do a function call, I return the expected data if it worked properly, or return array("error" => $error);
Then on receiving the returned data I check if isset($var['error']) and if its not then I know I received some expected data.
I would also appreciate if you could advice me if this a good or bad way of handling data between function calls? And if there is a better "good practice" for this.
Well, this is some of PHP misbehaviors, which luckily has been fixed in some recent version.
You can address a single character in a string using the same square braces used to address an array element.
'error' evaluates to 0 and then you have got $a[0] which is set.
to fix that you have to check if $a is array first
I believe it's a bug and it's fixed in PHP 5.4+: http://codepad.viper-7.com/fz1rnT
looks like isset($str[$key]) in same way as isset($str[intval($key)]), where $str and $key are strings
To handle errors best approach are exceptions:
http://php.net/manual/en/language.exceptions.php
I'm not 100% sure why the behavior is like this, but I do know that PHP allows you to handle strings in a similar way as an array.
$a = "StackOverflow";
echo $a[2]; // a
echo $a[4]; // k
echo $a[6]; // v
echo $a[8]; // r
Now when you pass a string key as an index of the array, PHP will try to parse that string into a numerical value to use as a key.
echo $a['stack']; // S
echo $a['over']; // S
echo $a['flow']; // S
echo $a['0stack']; // S
echo $a['1over']; // t
echo $a['2flow']; // a
echo $a['3flow']; // c
echo $a['4flow']; // k