How does PHP evaluate compound if statements?

Given the nested if statements below: if(file_exists($fullPathToSomeFile)) { if(is_readable($fullPathToSomeFile)) { include($fullPathToSomeFile); } } how does this differ from: if(file_exists($fullPathToSomeFile) && is_readable($fullPathToSomeFile)) { include($fullPathToSomeFile); } Specifically, I want to know how PHP will treat is_readable() if $fullPathToSomeFi

PHP如何评估复合语句?

鉴于下面的嵌套if语句: if(file_exists($fullPathToSomeFile)) { if(is_readable($fullPathToSomeFile)) { include($fullPathToSomeFile); } } 这与以下内容有何不同: if(file_exists($fullPathToSomeFile) && is_readable($fullPathToSomeFile)) { include($fullPathToSomeFile); } 具体来说,如果$ fullPathToSomeFile不存在(第一个条件失败),我想知道PHP如何处理is_readable()。

PHP isset returns true but should be false

My project is in cakePHP but I think this is an aspect of native PHP that I am misunderstanding.. I have an afterFind($results, $primary = false) callback method in my AppModel . On one particular find if I debug($results); I get an array like this array( 'id' => '2', 'price' => '79.00', 'setup_time' => '5', 'cleanup_time' => '10', 'duration' => '60', '

PHP isset返回true,但应该是false

我的项目在cakePHP中,但我认为这是我误解的本地PHP的一个方面。 我有一个afterFind($results, $primary = false)回调在我的方法AppModel 。 在一个特定的发现,如果我debug($results); 我得到这样的数组 array( 'id' => '2', 'price' => '79.00', 'setup_time' => '5', 'cleanup_time' => '10', 'duration' => '60', 'capacity' => '1', 'discontinued' => false, 's

How to log errors and warnings into a file?

How to turn on all error and warnings and log them to a file but to set up all of that within the script (not changing anything in php.ini). I want to define a file name and so that all errors and warnings get logged into it. Use the following code: ini_set("log_errors", 1); ini_set("error_log", "/tmp/php-error.log"); error_log( "Hello, errors!" ); Then watch the file: tail -f /tmp/php-erro

如何将错误和警告记录到文件中?

如何打开所有错误和警告并将它们记录到文件中,但要在脚本中设置所有内容(不会更改php.ini中的任何内容)。 我想定义一个文件名,并且所有的错误和警告都会被记录下来。 使用下面的代码: ini_set("log_errors", 1); ini_set("error_log", "/tmp/php-error.log"); error_log( "Hello, errors!" ); 然后观看文件: tail -f /tmp/php-error.log 或从2008年的此博客条目中更新php.ini 。 看到 error_log - 在某处发送错

nan() throws a warning for strings

I've been using the is_nan() function (ie is-not-a-number) to check whether a variable taken from the query string is a number or not. However, in the case of the variable being a string (in which case is_nan() should return TRUE ), the function also throws the following rather annoying warning: Warning: is_nan() expects parameter 1 to be double, string given Since is_nan() is for checking

nan()会为字符串发出警告

我一直在使用is_nan()函数(即不是一个数字)来检查从查询字符串中获取的变量是否是数字。 但是,在变量是一个字符串的情况下(在这种情况下, is_nan()应该返回TRUE ),该函数也会引发以下相当恼人的警告: Warning: is_nan() expects parameter 1 to be double, string given 由于is_nan()用于检查变量是否不是数字,为什么它会为字符串引发错误? 我会认为它应该接受非数字参数,因为这是种类 - 它的目的... 为什么会

How do I catch a PHP Fatal Error

I can use set_error_handler() to catch most PHP errors, but it doesn't work for fatal ( E_ERROR ) errors, such as calling a function that doesn't exist. Is there another way to catch these errors? I am trying to call mail() for all errors and am running PHP 5.2.3. Log fatal errors using register_shutdown_function , which requires PHP 5.2+: register_shutdown_function( "fatal_handler"

如何捕获PHP致命错误

我可以使用set_error_handler()来捕获大多数PHP错误,但它不适用于致命( E_ERROR )错误,例如调用一个不存在的函数。 是否有另一种方法来捕捉这些错误? 我正在尝试为所有错误调用mail() ,并且正在运行PHP 5.2.3。 使用register_shutdown_function记录致命错误,它需要PHP 5.2+: register_shutdown_function( "fatal_handler" ); function fatal_handler() { $errfile = "unknown file"; $errstr = "shutdown

Is there a static way to throw exception in php

Is there a "static" way of throwing an exception in php? I need to throw an exception when a mysql query fails. I tried this: $re=@mysql_query( $query ) or throw new Exception(' Query Failed '); but it's not working. And I'm using a function based on the throwException() function from this comment at PHP: Exceptions manual , but I would like to know if there is a static

有没有一种静态的方式来在PHP中抛出异常

有没有在PHP中抛出异常的“静态”方式? 当mysql查询失败时,我需要抛出异常。 我试过这个: $re=@mysql_query( $query ) or throw new Exception(' Query Failed '); 但它不起作用。 我在PHP:Exceptions手册中使用了一个基于throwException()函数的函数,但是我想知道是否有静态的方法来做这个,而不需要创建一个类。 您将无法直接执行or throw new Exception(); 因为throw是一个陈述,而不是一个表达。 既然or真的

catch blocks: are they able to catch invalid arg types?

Background: Suppose I have the following obviously-incorrect PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception $exx){} For it to work with array_pop, $vtest should obviously be an array, not a string. Nevertheless, when I run this code the Warning is exhibited. I don't want that, I just want the code to fail silently. Question: Is there something

catch块:它们能够捕获无效的arg类型吗?

背景:假设我有以下显然不正确的PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception $exx){} 为了使用array_pop,$ vtest显然应该是一个数组,而不是一个字符串。 不过,当我运行此代码时,会显示警告。 我不想那样,我只想让代码默默地失败。 问题:与其他语言相比,PHP try-catch有什么特别之处,导致它不起作用? 免责声明:仅供参考,确实有其他方法可以在PHP中处理这种

Suppress error with @ operator in PHP

In your opinion, is it ever valid to use the @ operator to suppress an error/warning in PHP whereas you may be handling the error? If so, in what circumstances would you use this? Code examples are welcome. Edit: Note to repliers. I'm not looking to turn error reporting off, but, for example, common practice is to use @fopen($file); and then check afterwards... but you can get rid of

在PHP中用@操作符来抑制错误

在你看来,使用@操作符来抑制PHP中的错误/警告是有效的,而你可能正在处理错误? 如果是这样,你会在什么情况下使用它? 代码示例是受欢迎的。 编辑:注意到repliers。 我不打算关闭错误报告,但是,例如,通常的做法是使用 @fopen($file); 然后再检查......但你可以通过这样做来消除@ if (file_exists($file)) { fopen($file); } else { die('File not found'); } 或类似的。 我想问题是 - 有没有地方可以

function() must be an instance of string, string given" prior to PHP 7?

Here is my code: function phpwtf(string $s) { echo "$sn"; } phpwtf("Type hinting is da bomb"); Which results in this error: Catchable fatal error: Argument 1 passed to phpwtf() must be an instance of string, string given It's more than a little Orwellian to see PHP recognize and reject the desired type in the same breath. There are five lights, damn it. What is the equivalent of

函数()必须是字符串的一个实例,在PHP 7之前给出的字符串?

这是我的代码: function phpwtf(string $s) { echo "$sn"; } phpwtf("Type hinting is da bomb"); 这导致了这个错误: 可捕获的致命错误:传递给phpwtf()的参数1必须是字符串的实例,字符串是给定的 它不仅仅是一个小型的奥维尔人,可以看到PHP识别并相互拒绝所需的类型。 有五盏灯,该死的。 PHP中字符串的类型提示的等价物是什么? 附加考虑到解释到底发生了什么的答案。 概要 该错误消息令人困惑的原因

How can I remove 3 characters at the end of a string in php?

How can I remove 3 characters at the end of a string in php? "abcabcabc" would become "abcabc"! Just do: echo substr($string, 0, -3); You don't need to use a strlen call, since, as noted in the substr docs: If length is given and is negative, then that many characters will be omitted from the end of string <?php echo substr("abcabcabc", 0, -3); ?><?php ech

我如何删除在PHP字符串的末尾3个字符?

我如何删除在PHP字符串的末尾3个字符? “abcabcabc”会变成“abcabc”! 做就是了: echo substr($string, 0, -3); 您不需要使用strlen调用,因为如substr文档中所述: 如果给出长度并且是负数,那么很多字符将在字符串末尾被省略 <?php echo substr("abcabcabc", 0, -3); ?><?php echo substr($string, 0, strlen($string) - 3); ?>