Search code examples
phpfunction-parametertexttrimming

php function parameter is not retaining full string passed in


I have a PHP function that in echoing out a string, is losing some of the characters passed in it. Any reason why this would be happening?

I pass: $vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX

It returns: #jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX

This is my PHP code:

<?php
function display($str) {
    echo $str;
}
display("$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX");
?>

Solution

  • If you have $ in you string use single quotes:

    display('$vhkvdov#jqlydk#p*_L#1qrlws|ufqh#KWLZ#1hwdgsX');
    

    Otherwise PHP would try to use $vhkvdov as a variable name and replace it by its contents, what will be nothing as I expect that the variable is not set

    PHP reference on strings


    Note: During development I would set error_reporting in php.ini to

    error_reporting=E_ALL | E_NOTICE;
    

    and

    display_errors=On
    

    or

    log_errors=On
    error_log=PATH/TO/YOUR/LOG.file ; make sure this is writable by the web server
    

    To see an error message like :

    PHP Notice: Undefined variable: vhkvdov in