Doubt about PHP function 'end'

Asked

Viewed 137 times

3

My doubt is in $extensao, where it contains a parenthesis at the beginning and end. Why it is there?

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

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

move_uploaded_file ( $_FILES [ "img" ][ "tmp_name" ], "upload/".$imagem ) ;

1 answer

6


Just like that, the parentheses at the beginning and at the end do nothing!

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

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

Strict Standards: Only variables should be passed by Ference in

Now yes speaking of the parentheses that make the difference, to get the return(value) of explode() to become a family member:

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

Take the test by displaying all the errors 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 will be generated a Warning or treat warnings or these types of gambiarras, in which case it is only create an additional varietal and pass to the function.

Parentheses Around Function Parameters no longer affect behaviour


How to Grab a File Extension

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

*Obs tried to do an example in ideone, phpfidle and 3v4l.org, it seems that directive that enables all errors is offline.

  • Okay, thannks....

  • +1 for pathinfo. To make the bullshit object-oriented I would do $file = new SplFileObject($_FILES [ "img" ][ "name"]);

  • @Wallacemaxters, I do not know this, I will look later. It is not pq solves that you should solve the problem thinking that everything is text/string manipulation. Thanks for the tip :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.