ThinkPHP
ThinkPHP中的每一个xxxAction.class.php文件就代表着一个应用模块,这个Action中的每一个方法(function)代表着一个操作,操作可以分为有输出到模板的操作和只具执行不需要输出的操作。
打开Myapp/Lib/Action/IndexAction.class.php文件,我们可以看到里面的基础代码:
class IndexAction extends Action{ public function index(){ } }
对此,需要指出一下几点:
1.在ThinkPHP的开发中,要增加一个应用模块,就要在Action文件夹里建立一个类,类的文件命名格式是“模块名称+Action.class.php”。例如我们这里的应用模块是Index,所以定义文件名为IndexAction.class.php。
2.应用模块类的定义要继承框架的Action类。要为这个应用模块添加一个操作,则定义一个以此操作为命名的function.例如上面的index操作。
通常一个应用模块中,会有若干操作(function)需要有与用户交互的页面,这就需要用到模板输出,ThinkPHP本身已内置了一套具有ThinkPHP特色的,很强大易扩展但应用非常方便兼简单的模板引擎。
在应有模块中,如果某个操作是需要页面显示的,只要对应在Myapp/Tpl/default/里建立一个文件夹,文件夹以应用模块的名称来命名,然后在这个文件夹下,建立一个以这个function名称来命名的html文件,就可以在这个方法中使用$this->display()方法来直接调用该模板。(当然也可以调用其它模块下的其它模板或显式指定模板文件位置和名称,由于是循序渐进式的学习,就让我们先忽略吧)了解这些理论后,我们先简单实操一下这些知识。
(1)在Myapp/Tpl/default/下建立一个文件夹,根据应用模块的名称,我们将这个文件夹命名为Index
(2)在Myapp/Tpl/default/Index/下建立一个html文件,根据操作名称,我们命名该文件为index.html
(3)打开Myapp/Lib/Action/IndexAction.class.php文件,修改代码为
<?php class IndexAction extends Action{ public function index(){ $value = 'hello,ThinkPHP'; $this->assign('name',$value); $this->display(); } } ?>
(摘自手册:ThinkPHP模板指南,此后的知识要点均来自ThinkPHP官方手册,不再申明)
在Action类里面使用 assign方法对模板变量赋值,无论何种变量类型都统一使用assign赋值。
$this->assign('name',$value);
// 下面的写法是等效的
$this->name = $value ;
// 模板变量赋值后就需要调用模板文件来输出相关的变量,模板调用通过display方法来实现
$this->display();
4 打开Myapp/Tpl/default/Index/index.html文件,代码为
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>{$name}</title> </head> <body> 测试输出: {$name} </body> </html>
此处注意:模板变量使用{$变量名称}这种标签进行输出。
不同的模板变量类型,使用不同的标签,标签可以自行另外定义,暂且不理会。
5 打开浏览器输入地址:
附加补充知识:
1 如果要同时输出多个模板变量,可以使用下面的方式:
$array = array(); $array['name'] = 'thinkphp'; $array['email'] = '123456@vip.qq.com'; $array['phone'] = '123456'; $this->assign($array);
这样,就可以在模板文件中同时输出name、email和phone三个变量。
2 我们使用上面的变量定义,将整个数组定义为一个模板变量来输出
$array = array(); $array['name'] = 'thinkphp'; $array['email'] = '123456@vip.qq.com'; $array['phone'] = '123456'; $this->assign('array',$array); $this->display();
在html中,要输出$array['name']的值,代码是
{$array.name} 或 {$array['name']}
3 将这个数组循环输出
(1) IndexAction.class.php中代码更改如下
<?php class IndexAction extends Action{ public function index(){ $array = array(); $array['name'] = 'thinkphp'; $array['email'] = '123456@vip.qq.com; $array['phone'] = '123456'; $value = 'hello,ThinkPHP'; $this->assign('array',$array); $this->assign('name',$value); $this->display(); } } ?>
(2) 将Myapp/Tpl/default/Index/index.html代码更改如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>{$name}</title> </head> <body> <iterate name="array" id="vo"> {$vo}<br /> </iterate> </body> </html>
注意:name='array'是指要循环的模板变量是array,id='vo'是指这个数据在模板输出时所使用的名称

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor