Php explode and echo second element - php

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";
}

Related

PHP Undefined Arrays returning wrong values

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

PHP variables variable not displaying if passed as an array or object

This works with simple variables. But it shows empty result with complex variables. AM I MISSING SOMETHING HERE? or is there anyother way around. Thanks.
#1. This works with simple variables.
$object = "fruit";
$fruit = "banana";
echo $$object; // <------------ WORKS :outputs "banana".
echo "\n";
echo ${"fruit"}; // <------------ This outputs "banana".
#2. With complex structure it doesn't. am I missing something here?
echo "\n";
$result = array("node"=> (object)array("id"=>10, "home"=>"earth", ), "count"=>10, "and_so_on"=>true, );
#var_dump($result);
$path = "result['node']->id";
echo "\n";
echo $$path; // <---------- This outputs to blank. Should output "10".
Not exactly using variable variables, but if you want to use a variable as the var name, eval should work
$path = "result['node']->id";
eval("echo $".$path.";");
From php.net's page on variable variables
A variable variable takes the value of a variable and treats that as the name of a variable.
The issue is that result['node']->id is not a variable. result is the variable. If you turn on error reporting for PHP notices you will see the following in your output:
PHP Notice: Undefined variable: result['node']->id ...
This can be solved as follows:
$path = "result";
echo "\n";
echo ${$path}['node']->id;
The curly braces around $path are required.
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
If not present the statement is equivalent to
${$path['node']->id}
which will result in the following output:
PHP Warning: Illegal string offset 'node' in /var/www/html/variable.php on line 18
PHP Notice: Undefined variable: r in /var/www/html/variable.php on line 18
PHP Notice: Trying to get property of non-object in /var/www/html/variable.php on line 18

How to fetch the substring

In my code I have a statement like this:
$last_100_char = substr($str[0], -100);
I want to retrieve the last 100 characters of the first element of an array, but while doing this I,m getting some error
substr() expects parameter 1 to be string, array given.
how to fix this?
The error has occurred because the first parameter ie, $str[0] is still an array. As another more efficient method, to obtain the first element of an array you can use array_shift() method.
$first=array_shift(array_values($array));
echo substr($first, -100);
Your example is working fine when I test it.
Make sure that your $str[0] actually contains a string by doing print_r( $str );
I got the answer
Initially I did
$str[] = explode(';',$job_record ['JD']);
$last_100_char = substr($str[0], -100);
So I'm getting that error..
Now I made it as
$str['data']=explode(';',$job_record ['JD']);
$last_100_char = substr($str['data'][0], -100);
Now I'm getting the correct output...

PHP "continue" stops foreach loop

This seems like a very stupid question, but without making any changes to the server.. the continue function in PHP seems to have started working incorrectly.
For example:
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
continue;
echo $test."<br>";
}
}
Outputs:
Got here
Got here
Got here
Got here
Whereas:
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
echo $test."<br>";
}
}
Ouputs:
Got here
1
Got here
3
Got here
4
Got here
5
I have used this function before and it did not seem to have this effect. Any ideas? Like I said, nothing on the server has changed so the PHP version is the same.
I don’t know what effect you want, but this example is working correctly. continue needs to break current iteration and go to the next without executing code below this operator. And this function works in this case all the time since PHP 4.
I think you need to understand how continue works. i wanted to add some, so that if some body else confronts the same, may have this as reference.
You need to use the key word continue When you want ignore the next iteration of a loop. continue is always used with if condition
According to example here .
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
**continue;**
echo $test."<br>";
}
}
This works as expected and perfect and here is why.
You are looping through the array $testers which has four elements inside and
after getting every element , you are telling php to ignore the elements by using
continue and that is the reason why it will not output the elements of the array
$testers.
Let me try to rewrite your Example here.
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
if ($test == 1):
continue;
endif;
echo $test."<br>";
}
}
echo contTest();
I have just used continue if element is equal to 1, meaning that element will be
skipped (ignored).
The output is :
Got here
Got here
3
Got here
4
Got here
5
As you can see 1 is ignored.
The continue basically says ignore the rest of the code after continue and start with the next step of the foreach loop. So the result you are getting is perfectly okay (see http://www.php.net/manual/de/control-structures.continue.php). There must be some other effect that changed your output.
This is what continue does exactly:
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
-- http://www.php.net/manual/en/control-structures.continue.php

pass values using header in php

1.php
<?php
header( 'Location: 4.php?$x=1&y=2&z=3' );
?>
sends the value of x ,y ,z
4.php
<?php
print '<pre>';
$a= $_GET ;
echo $a[x];
print '</pre>';
?>
when we call 1.php it is redirected to 4.php
it displays the value of x correct but it gives error
Notice: Use of undefined constant x - assumed 'x' in C:\wamp\www\4.php on line 6
why it gives error ?
Common bug again...
echo $a[x];
should be
echo $a['$x'];
in echo $a[x];, x is treated as (so called) "bare string", and PHP will look for a constant named x, which does not exist.
On the other hand, you need to get the $x key in the $_GET superglobal, which is populated by PHP from your URL.
You need this instead:
echo $a['$x']
Note that you're passing in $x in the query string. Make sure you use the appropriate string key of $_GET, or $a in your case.

Categories