PHP优化代码

PHP优化代码,php,Php,关于“此操作的最短代码是什么?”的几个问题: 没有{} if($match->name == "Marry") { $match = true; } if($match->name == "Marry") { $match = true; } else { $match = false; } 没有{} if($match->name == "Marry") { $match = true; } if($match->name ==

关于“此操作的最短代码是什么?”的几个问题:

  • 没有{}

    if($match->name == "Marry") {
        $match = true;
    }
    
    if($match->name == "Marry") {
        $match = true;
    } else {
        $match = false;
    }
    
  • 没有{}

    if($match->name == "Marry") {
        $match = true;
    }
    
    if($match->name == "Marry") {
        $match = true;
    } else {
        $match = false;
    }
    
  • 如果$match=true,如何询问

  • 如何询问数组()是否有任何值

  • 如何询问$variable是否有数值(0-9)

  • 如果$variable的第一个符号不是字母或数字,如何询问

  • 如果$match=false,如何询问

  • (一)

    这已经足够短了,你不能在不影响可读性的情况下更改它(除了可能移除大括号)

    (二)

    请注意,您使用的是相同的变量名

    (三)

    我想您想要
    if($match==true)
    ,应该只写
    if($match)

    (四)

    EDIT我将其解读为“具有任何(=某些)特定值”,以检查数组是否为空,您可以使用它)

    (五)

    有几种方法,一种可能的方法是

    is_numeric($variable) && strlen("$variable") == 1
    
    要允许启动0,您可以执行以下操作

    is_numeric($variable) && $variable >= 0 && $variable <= 9
    
    (一)

    (二)

    (三)

    (四)

    (五)

    (六)


    A1。不能真正缩短,在第2部分的情况下接受

    A2。 $match=($match->name==“mary”)

    A3。我猜你的意思是

    if ($match) {
       echo "Value of $match evaluates as true";
    }
    
    if (empty($array)) {
    // or 
    if (in_array($variable, $array)) {
    
    A4。不知道你是不是说

    if ($match) {
       echo "Value of $match evaluates as true";
    }
    
    if (empty($array)) {
    // or 
    if (in_array($variable, $array)) {
    
    A5

    A6


    1和2应该是
    Withous{}
    。但我不同意hsz。缺少它们会导致令人头疼的奇怪代码功能。优化内存中字节码的最小大小?最快的执行?或者开发人员可以输入的最少字符数?请让它“优化以便于项目中的其他编码人员理解”。我必须破译“看,我能在一行代码中破译!”胡说八道并不好笑。再说一次,在我早年的日子里,通常是我把单线意大利面放在那里的。我活该。
    if ( $match === true ) {}
    
    $array = array();
    if ( !empty( $array ) ) {}
    
    if ( is_numeric( $variable ) ) {}
    
    if ( ctype_alnum($variable[0]) ) ) {}
    
    1) OK
    2) $match = ($match->name == 'Marry';
    3) if ($match)
    4) if (count($array))
    5) preg_match('/^[+-]?\d+(\.\d+)?$/', $string)
    6) preg_match('/^[^0-9a-zA-Z]/', $string)
    
    if ($match) {
       echo "Value of $match evaluates as true";
    }
    
    if (empty($array)) {
    // or 
    if (in_array($variable, $array)) {
    
    if (is_numeric($variable)) {
       echo "it's numeric";
    }
    
    if (preg_match('#^[^a-z\d]#i', $variable) {
       echo "doesn't start with letter or number";
    }