Good day.

The point is: there is some variable that receives the address of the picture, which is then processed by the function and returned, but the point is that when the return occurs, the value becomes zero.

Here is the php code:

<?php $img="http/site.ru/1.img"; var_dump($img); function strReplase($imgThe){ $imgUrl=str_replace(".jpg","-150x150.jpg",$imgThe); return $imgUrl; } var_dump(strReplase($img);?> 

In response, I get:

 string(18)"http/site.ru/1.img" null 

Prompt, please, why value is not transferred from function. The code is not quite alive, so ochepyatki possible somewhere.

And if you put

  return var_dump($imgUrl); 

it will work out

  string(18)"http/site.ru/1.img" string(25)"http/site.ru/1-150x150.img" null 

that is, the function processes the data.

Then asked to lay out this function.
At the moment on the site (of course, this is a test for verification) I have placed the following code here:

  function srtReplace($img){ $imgUrl=str_replace(".jpg","-150x150.jpg",$img); return $ImgUrl;} $theOneImg="http://site.ru/one.jpg"; $oneThumb=srtReplace($theOneImg); var_dump($oneThumb); 

The result is null.

    2 answers 2

    1) The function must return a value. Return comes through return $ imgUrl; Your var_dump string returns the result of the var_dump function.

    2) In the function call, you passed the wrong variable and did not close the bracket.

    var_dump (strReplase ($ img));


    Here is the code:

     <?php $img="http/site.ru/1.img"; var_dump($img); function strReplase($imgThe){ $imgUrl=str_replace(".jpg","-150x150.jpg",$imgThe); var_dump($imgUrl); return $imgUrl; } var_dump(strReplase($img)); // Тут изменил $imgThe на $img ?> 

    Displays:

     string(18) "http/site.ru/1.img" string(18) "http/site.ru/1.img" string(18) "http/site.ru/1.img" 

    In your next version:

     function srtReplace($img){ $imgUrl=str_replace(".jpg","-150x150.jpg",$img); return $imgUrl; // изменил $ImgUrl на $imgUrl } $theOneImg="http://site.ru/one.jpg"; $oneThumb=srtReplace($theOneImg); var_dump($oneThumb); 

    Conclusion:

     string(30) "http://site.ru/one-150x150.jpg" 
    • Etki and IVsevolod in the front of us for some reason I can not find the button to add comments. I do not want to insult you, but still you do not see I edited the question? In the return var_dump (...) function, I just wrote to show that $ imgUtl has a value, but it does not get through return. I edited the question because in the original code return var_dump (...) is not worth it, but immediately return $ imgUrl and the value is not output by the var_damp function. And about this in the comments to the previous answer wrote that it is written here for verification. - Sergalas
    • @Sergalas, and I repeat once again - it does not happen that the var_dump leaves, and the return does not leave. Remove var_dump, check three times, if it didn’t start up, post a really non-working code. - etki
    • @Sergalas, try the code in the answer, what will output? Once again, with the new code - IVsevolod
    • Everything, I found, in what a mistake, I give you the answer. For perseverance thanks also @Etki. - Sergalas

    Well, what you ask is what you get. Also change

     return var_dump($imgUrl); 

    on

     return $imgUrl; 

    You decide - you need to either output the result in the function, or return it from it?

    • You really do not try. You think I only displayed var_dump for me to check and see. Why and where is the mistake. - Sergalas
    • one
      @Sergalas, then why do you even have an incorrect code in the example, if you know about it? - Emil Sabitov
    • I showed in the example that the code is processed even before the transfer, but not transmitted. How could I reflect this? I can remove wards from the back - the result will not change. - Sergalas
    • @Sergalas, this is not a sishka, it does not happen here, that there is something in the output of the function, but it does not go away through return. - etki
    • @Sergalas, now you understand why the parameter is not passed? ) - IVsevolod