dra87370 2013-03-24 03:19 采纳率: 100%
浏览 25
已采纳

PHP语法 - 打破一行参数并返回/ echo

Using arrays, I see quit often the following formatting :

$somearray = array( 'one'=>'one_value',
                    'twokey'=>'two_value',
                    'some_third_value'=>'some_other_value',
                    '987654'=>'12340987',
                    'confused_yet_456789'=> $some_var,
                    'confused_yet_rt78466'=>'two_value'
                    );

that is of course instead of the one liner :

$somearray = array( 'one'=>'one_value','twokey'=>'two_value','some_third_value'=>'some_other_value','987654'=>'12340987','confused_yet_456789'=> $some_var,'confused_yet_456789'=>'two_value');

However , I for myself have never encountered or seen it used in a functions $parameters assignment, e.g :

function my_function_3($somevar='some_value_or_other',$title='some_87654_num_value',$cool_stuff='right_or_left',$dashboards='91234765',$menu='some_6374_menu'){

return $cool_stuff;

}

Expanded to :

function my_function_3( $somevar='some_value_or_other',
                        $title='some_87654_num_value',
                        $cool_stuff='right_or_left',
                        $dashboards='91234765',
                        $menu='some_6374_menu'
                        ){
return $cool_stuff;
}   

The same goes for echo or return.

From this :

$output .= '<img src="http://chart.apis.google.com/chart?cht=qr&chs='.$qr_size .'x'.$qr_size .'&chld=L|4&chl=' .  $string . '" width="'.$qr_size.'" height="'.$qr_size.'" alt="Some alt to describe QR code" title="QR code for your phone"/>';

to this :

 $output .= '<img src="http://chart.apis.google.com/chart?cht=qr&chs='.
                $qr_size .'x'.
                $qr_size .'&chld=L|4&chl=' .  
                $string . '" width="'.
                $qr_size.'" height="'.
                $qr_size.'" alt="Some alt to describe QR code" title="QR code for your phone"/>';

So , my question is :

Can I use this syntax or formatting also in $parameters assignment in a function, as well as echo / return ?

if no - why ? can it fail on certain configs ? is it just wrong ?

..and if yes , what is the correct way to do so (break the line BEFORE or AFTER the comma / dot ) ??

  • 写回答

1条回答 默认 最新

  • dsj2014 2013-03-24 04:45
    关注

    Yes you can use new lines anywhere it PHP. They don't have any syntactic meaning like they do in Javascript (where a new line means terminate the current statement) or Java where new lines can't be used within strings.

    In fact all whitespace is trimmed within PHP, not just newlines, but spaces and tabs as well.

    what is the correct way to do so

    That is a matter of taste but I prefer to have the dot/comma before the new line, as that produces code that is easiest to read.

    Slightly more controversially, if the parameters won't fit on a single line easily, I also prefer to have the multi-line parameters use one tab more indentation than the code they're in, rather than aligning with the bracket of the function.

    $arg1 = simpleFunction();
    $arg2 = simpleFunction2($arg1);
    
    $result = testFunctionWithManyParameters(
      $arg1,
      $arg2,
      $arg3,
      $arg4,
      $arg5
    );
    

    As again, imho, it makes reading the code easier than having them indented massively.

    $result = testFunctionWithManyParameters($arg1, $arg2, $arg3, $arg4, $arg5 );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应