PHP 和未定义变量策略 [英] PHP and undefined variables strategy

查看:40
本文介绍了PHP 和未定义变量策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名从 PHP 开始的 C++ 程序员.我发现由于未定义的变量,我失去了大部分调试时间(以及我的自尊!).据我所知,处理它们的唯一方法是在执行时观察输出.

I am a C++ programmer starting with PHP. I find that I lose most of the debugging time (and my selfesteem!) due to undefined variables. From what I know, the only way to deal with them is to watch the output at execution time.

是否有其他策略可以更早地注意到这些错误(类似于 C++,一次编译即可提供您需要的所有线索)?

Are other strategies to notice these faults earlier (something like with C++ that a single compile gives you all the clues you need)?

推荐答案

这是 PHP 的常见抱怨.以下是一些想法:

This is a common complaint with PHP. Here are some ideas:

  1. 使用代码分析工具.许多 IDE,例如 NetBeans 也会有所帮助.

  1. Use a code analysis tool. Many IDEs such as NetBeans will help also.

只需运行代码.PHP 没有像 C++ 那样昂贵的编译步骤.

Just run the code. PHP doesn't have an expensive compilation step like C++ does.

使用单元测试.常见的副作用包括:更好的代码.

Use unit testing. Common side effects include: better code.

设置error_reporting(-1)ini 文件.

获取 xdebug.这不是预防性的,但堆栈跟踪有助于消除错误.

Get xdebug. It's not preventative, but stack traces help with squishing bugs.

isset(), === null(身份运算符)和保护子句是您的朋友.

isset(), === null (identity operator), and guard clauses are your friends.

松散和动态类型是该语言的一个特性.仅仅因为 PHP 对打字不严格并不意味着你不能.如果它真的困扰你并且你有选择,你可以尝试使用 Python 代替——它对输入更严格一些.

Loose and dynamic typing are a feature of the language. Just because PHP isn't strict about typing doesn't mean you can't be. If it really bugs you and you have a choice, you could try Python instead—it's a bit stricter with typing.

这篇关于PHP 和未定义变量策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆