Clear definition for the PHP str_pad() Function

前端 未结 2 516
借酒劲吻你
借酒劲吻你 2021-01-26 02:49

what is the effect of the function str_pad() to strings? please explain to me because I cant understand its definition from w3schools. Thanks.

2条回答
  •  既然无缘
    2021-01-26 03:22

    It pads a string to a certain length with (optional) another string.

    For example

    $foo = 'hello world'; // length 11
    echo str_pad($foo, 13); // will echo 'hello world  ';
    

    You can also add a string for padding

    $foo = 'hello world';
    echo str_pad($foo, 13, 'x'); // will echo 'hello worldxx';
    

    Moreover you can specify where you want the string to be padded

    $foo = 'hello world';
    echo str_pad($foo, 13, 'x', STR_PAD_LEFT); // will echo 'xxhello world';
    
    $foo = 'hello world';
    echo str_pad($foo, 15, 'x', STR_PAD_BOTH); // will echo 'xxhello world';
    

    However, never use w3schools for PHP but try to understand it directly from PHP documentation

提交回复
热议问题