Doubts about PHP function 'end'

3

My question is in $extensao , where it contains a parenthesis at the beginning and at the end. What's the point of being there?

$extensao = ( end ( explode ('.', $_FILES [ "img" ][ "name"] ) ) ) ;

$imagem = md5 ( uniqid ( $_FILES [ "img" ][ "name" ] ) ).'.'.$extensao ;

move_uploaded_file ( $_FILES [ "img" ][ "tmp_name" ], "upload/".$imagem ) ;
    
asked by anonymous 01.09.2015 / 23:09

1 answer

6

Exactly like this, the parentheses at the beginning and at the end do nothing!

$extensao = ( end ( explode ('.', $_FILES [ "img" ][ "name"] ) ) ) ;
 -----------^                                                    ^----   

The small problem with this code is that from php5.3 it generates a Strict Standards that basically is a warning, the message is as follows:

  

Strict Standards: Only variables should be passed by reference in

Now, speaking of the parentheses that make the difference, to force the return (value) of explode() to make a reference add the parentheses:

$extensao =  end ( ( explode ('.', $_FILES [ "img" ][ "name"] ) ) )  ;
-------------------^                                            ^----

Take the test by displaying all errors in this way:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
$extensao =  end( (explode ('.', 'arquivo.ext' ) ) );
$extensao =  end(explode ('.', 'arquivo.ext' )) ;

Update PHP7

Now in PHP7, even with these additional parentheses a warning will be generated, ie treat warnings or these types of gambiarras, in this case it is only necessary to create an additional variable and move to the function.

Parentheses around function parameters no longer affect behavior

How to grab the extension of a file

The most correct way to get the extension is to use pathinfo() as shown this answer .

* I tried to make an example in ideone, phpfidle and 3v4l.org, it seems that directive that enables all errors is turned off.     

01.09.2015 / 23:20