Skip Navigation

[Resolved] PHP Notice: Only variables should be passed by reference

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm encountering a PHP notice: "Only variables should be passed by reference" due to code in my check_filetype.php file. The code, created years ago, is producing this warning in the logs.

Solution:

This warning occurs because PHP prefers variables over direct function calls in end() statements. To resolve this, assign the results of explode() functions to variables before using end(). Here’s an updated version of your code:

<?php
/**
 * Check if the user uploaded job CPT file is a pdf.
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
add_shortcode( 'check_filetype', 'func_check_file_type' );
function func_check_file_type( $atts ) {

    $atts = shortcode_atts( array(
        'url' => '',
    ), $atts );

    $url_parts = explode( '/', $atts['url'] );
    $file_name = end( $url_parts );
    $file_extension = explode( '.', $file_name );

    return end( $file_extension );
}

Explanation of Changes:

- Saved the results of explode() into $url_parts and $file_extension.
- Applied end() on these variables rather than directly on the function calls.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Christopher Amirian 6 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2779405

I am trying to:

Link to a page where the issue can be seen:
Error is in log file:
[24-Oct-2024 19:21:56 UTC] PHP Notice: Only variables should be passed by reference in /home/204758.cloudwaysapps.com/kfgjchrfem/public_html/wp-content/toolset-customizations/check_filetype.php on line 16

I was help by one of your staff several years ago for this code and now I don't know how to fix it:
The code with the problem: is this:

<?php
/**
* Check if the user uploaded job CPT file is a pdf.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_shortcode( 'check_filetype', 'func_check_file_type');
function func_check_file_type($atts){

extract( shortcode_atts( array(
'url' => '',
), $atts ) );

$file_name = explode(".",end(explode('/',$url)));
return end($file_name);
}

#2779553

Christopher Amirian
Supporter

Languages: English (English )

Hi,

The PHP notice you're encountering is due to passing the explode() function's result directly into another end() function, which can lead to this warning. PHP prefers variables over direct function calls in such cases. We can fix this by separating the function calls into variables. Here’s an updated version of your code that should eliminate the warning:

<?php
/**
 * Check if the user uploaded job CPT file is a pdf.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_shortcode( 'check_filetype', 'func_check_file_type' );
function func_check_file_type( $atts ) {

    $atts = shortcode_atts( array(
        'url' => '',
    ), $atts );

    $url_parts = explode( '/', $atts['url'] );
    $file_name = end( $url_parts );
    $file_extension = explode( '.', $file_name );
    
    return end( $file_extension );
}

Explanation of Changes

- Stored the result of the first explode() function in $url_parts to separate the filename from the URL.

- Stored the result of the second explode() function in $file_extension to get the extension.

- Used end() on variables rather than directly on function calls.

Thanks.

#2779657

Thank YOU!!
Works like a charm!
Laura