Before delimiter with error_reporting

<?php

if(1==2) {
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
}
$varText='I like the boy';

$beferLike=array_shift(explode('like', $varText)); 
echo $beferLike;

I have the code above.
And the result of it is “I”.

But when I make the error_reporting work by changing “if (1==2)” to “if (1==1)”, it says the following.

Although I changed the error part like the following, it still says “Strict Standards …”

from
$beferLike=array_shift(explode('like', $varText)); 

to
$delimiter="like";
$beferLike=array_shift(explode( $delimiter, $varText)); 

strict_types declaration must be the very first statement in the script

What is the REAL problem you are trying to solve?

Additionally, the error reporting settings should be set in the php.ini file.

I like to get the beforeDelimeter “I” by a delimiter “like” from a variable “I like the boy”.

Hi @joon1,
Firstly, you are misusing the term delimiter which will confuse people reading this.

If I understand correctly you want to return the first I from the string. If that is not right,then show the expected output.

<?php
$string= 'I like the boy';
echo $string[0];

The OP is apparently using a php5 version and the error output is solely the result of the php error related settings being switched on/off, not the strict_type declaration. If he did have the php error related settings in the php.ini (or before any of the posted code), the declare(strict_types=1) statement would be giving a - Warning: Unsupported declare ‘strict_types’ in … error message. The ‘Only variables should be passed by reference…’ error also changed from a Strict Standards error to just a Notice in php7.0.

The reason for the - Only variables should be passed by reference… error is because explode returns an array, which you are not assigning to a program variable first, then passing this as an input to the array_shift(). Since there is no explicit variable holding the array, php is supplying a reference to the value on the internal data stack. To avoid this error, you will need to assign the result from the explode() to a variable, then supply this variable as an input to the array_shift(). Untested, but function array de-referencing should work - $beferLike = explode(‘like’, $varText)[0];

Good call. I forgot OP is bouncing between Php5 and Php7.

Just tested my above supposition about function array de-referencing and it does work without producing any error.

Note: strict_types only comes into play when php coerces a type. For the error the OP is getting, the type is not a problem, just how it is being supplied from the result of one function to the input of the next.

I would be tempted to search for the string $like=“like” :

<?php 
declare(strict_types=1);
// Both the follwoing should be set in php.ini
   error_reporting(-1); 
   // ini_set('display_errors', '1'); 

define('LOCALHOST', 'localhost'===$_SERVER['SERVER_NAME']);
  $tmp = LOCALHOST ? '1' : '0'; 
  // EITHER TO SCREEN OR error_log
  ini_set('display_errors', $tmp); 

$aLikes = [
  'I like the boy' ,
  'Today I like the boy',
  'Tomorrow I like the boy' ,
  'Yesterday I like the boy' ,
  'Every day I will like the boy'
];  

foreach($aLikes as $key => $like): 
  $aTmp = explode(' ', $like);
  for($i2=0; $i2<count($aTmp); $i2++) :
    if('like'===$aTmp[$i2]):
      echo '<br>strpos($like, "like") = '; 
      echo strpos($like, 'like');
      echo '&nbsp; &nbsp; $like = <b>"'    .$like .'"</b>';
      echo '<br>';
    endif;
  endfor;  
endforeach;      

Output:

This works:

$varText = 'I like the boy';

$exploded = explode('like', $varText);
$beforeLike = array_shift($exploded); 
echo $beforeLike;

The thing is you need to pass array_shift a variable, not directly feed it the result of another function.

However, if you just want anything up to like I would be tempted to use substr with strpos instead:

$varText = 'I like the boy';

echo substr($varText, 0, strpos($varText, 'like'));

Basically this means take the string $varText from position 0 until the position where you find the word like.

1 Like

The code above works fine and is easy to understand without any array concept.
Thank you.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.