弱类型好吗?好在哪,小弟我不知道
弱类型好吗?好在哪,我不知道
因公需要,搞起了php,它越来越让我觉得反胃.
代码里到处都是if empty if empty
if(!empty($CFG->aaaa)){}
搜索一下$CFG->aaaa从未出现过,当场吐血,然后多吐了几次就不吐了.
另外,到处都是以下形式的代码,反胃
if(){}else{
if(){
}else{
}
}
最让我郁闷的是
$a = NULL;
$a->a = 'bbb'; 这样尽然没错,NULL是没有意义的,完全就可以不使用stdClass了
弱类型在javascript中演绎得如此优雅,而在php中却如此恶心
从此我爱上了js中的未定义变量异常,至少解释器提示了我.
这种不声明便可以使用的变量给调试,阅读带来了无尽的痛苦.
抱怨归抱怨,血还是要接着吐,请高手指教php编程之道,这样或许能少吐.
php的编程模式是否是显示页面与处理页面分离?
还是都同一个页面,使用if else判断?
如果是分离的,如何实现服务器端跳转? 即在一次请求内执行流程经过多个页面,require或include?
------解决方案--------------------
强类型还是弱类型,都有各自的优缺点,不能一概而论,强类型编译质量高,代码运行快。弱类型则更灵活。
尽管php是弱类型语言,但是,良好的实践告诉我们,使用变量前先初始化是一个良好的习惯,不仅便于阅读,而且可以避免很多莫名其妙的错误。
而且,php开发的优点就是快速,轻便,如果使用强类型,恰恰违反了它的初衷。
------解决方案--------------------
处理页面和展现页面还是分开比较好吧?至于跳转现在基本上都是用框架。。。我也是刚转到php不久的,弱类型的确让我很不习惯,但我超喜欢用关键字做键的数组,很好用~
------解决方案--------------------
如果觉得不习惯,记得自己写的代码中变量初始化一下
------解决方案--------------------
php的编程模式是否是显示页面与处理页面分离?
还是都同一个页面,使用if else判断?
php有很多优秀的mvc框架
------解决方案--------------------
PHP的弱类型使用起来还是很方便的。至于你觉得它恶心,那只是你没上手。
起码,弱类型使用起来便洁,现在我们用OOP模式开发,可以做到将成员属性集中在类中声明,这样便于管理。
再想,一个页面中,变量的个数毕竟是有限的,还不至于让人无法追踪变量的地步。
如果你真不习惯它这种模式,那你可以这样:开启PHP的错误级为所有,然后将变量集中起来初始化,这样就行了。
eg:
- PHP code
<?php error_reporting(E_ALL); $a='a';$b='b'; echo $a; echo $c; //$c没有初始化,所以会抛出NOTICE级错误提示,便于你规范化代码。 ?> <div class="clear"> </div>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
