Jump to content
Riko

help with 5.3 code to 7.1 code

Recommended Posts

Hello,

The following code is part of larger script that worked in php 5.3

 //Translate string ($str) to UTF-8 from given charset
  function strToUtf8($str){
    $chars = unpack('C*', $str);
    $cnt = count($chars);
    for($i=1;$i<=$cnt;$i++) {
//echo "Char->" . $chars[$i];
    $this->_charToUtf8(&$chars[$i]);
//echo " utf8->" . $chars[$i] . "<br>";
    }
$returnstr = implode("",$chars);
//echo "Return after implode() " . $returnstr;
    return $returnstr;
  }


However:

Under php 7.1 i get a syntax error ( syntax error, unexpected '&' in your code )  on this line:

$this->_charToUtf8(&$chars[$i]);

 

Can someone suggest changes for the code so it will work under 7.1?

 

Thanks in advance

GeeJay

 

 

 

 

 

Share this post


Link to post
Share on other sites
Posted (edited)

You can't pass arguments by reference in the function call. The "by reference" should be defined in the function definition

EG

// define function
function my_func(&$chr)
{
  // do something to $chr
  ++$chr;
}

//  call function
$x = "A";
echo $x;                        // A
my_func($x);
echo $x;                        // B

(I am surprised that that 5.3 didn't have an adverse reaction to your code. I thought that restriction had been around longer than that)

Edited by Barand
code revision

Share this post


Link to post
Share on other sites
26 minutes ago, Barand said:

(I am surprised that that 5.3 didn't have an adverse reaction to your code. I thought that restriction had been around longer than that)

You're right, it was deprecated in 5.3 and removed in 5.4. OP probably had warnings hidden.

Share this post


Link to post
Share on other sites

Hello,

Thank you for your quick answers.

I must tell you that I am an absolute noob with php (You probably guessed that already).

Could you give an example of what the code would be in 7.1

Greetings

 

Share this post


Link to post
Share on other sites
1 hour ago, Riko said:

Could you give an example of what the code would be in 7.1

I already have.

Share this post


Link to post
Share on other sites
On 7/14/2019 at 8:35 AM, Riko said:

Could you give an example of what the code would be in 7.1

Also note that the link provided by requinix contains examples of passing by reference.

In case you're not aware, PHP can be set to display all errors and warnings. The settings are helpful during the development / debugging process. Just be sure to change the settings back to where errors / warnings are not displayed publicly once your script is live. The messages aren't user friendly and can aid malicious actors. More information about showing / hiding errors and warnings can be found here:
https://www.php.net/manual/en/function.error-reporting.php

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.