Three good boys First Php

Three good boys
HomeBackend DevelopmentPHP Tutorial[php learning 2] Basic grammar exercises 1

Since the results are almost filling one page, let’s record them first~!



    <meta charset="UTF-8">
    <title>First Php</title>


<h1 id="三好少年">三好少年</h1>
<?php /*基本语法练习
*1,定义变量必须加$,但不用写类型.这就导致了定义和赋值是一样的语句了.
*/
$x = 5;
$y = 10;
function Test(){
    global $x , $y;/*访问全局变量必须加GLOBAL*/
    static $z = 0;/*静态局部变量*/
    $y = $y + $z;
    $z++;
}
Test();
Test();
echo $y,&#39;<br>';//11

/*
 * 1,echo没有返回值,可以输出多个字符串,逗号隔开~
 * 2,Print总返回1,只能输出一个字符串~
 * 3,单引号只能输出字符串,双引号能输出变量~
 * 4,var_dump这个犀利啊,能监测变量类型,并输出值.
 * 5,数组元素可以不同类型,这个语法也是没谁了.
*/
$cars = array("Volvo","BMW","TOYOTA",5);
echo "字符串1","字符串2",'字符串3';
echo '<h2 id="PHP-IS-Fun">PHP IS Fun</h2>';//还能输出标签~
echo "$cars[0]<br>";
print("print只能一个字符串<br>");
print("print也能输出标签<h2 id="print标签">print标签</h2>");
var_dump($cars); print '<br>';

/*
 * 1,常量的定义真不怎么样,默认区分大小写,想不区分的话,最后参数为False;
 * 2,print(china_display + constInt + '<br>');这个有错误,但能运行,只显示800;看来这个编译器不是很严格.~
 * 3,常量默认全局的,函数调用的话不用Global修饰;
 * */
define("china_display","中华人民共和国");
define("constInt",800);
echo china_display,constInt,'<br>';
function TestConst($a){
    echo china_display, $a, '<br>';
}
TestConst("伟大");

/*
 * 1,两个字符串链接有个叫"并置运算符"用.标示,链接两个字符串.~
 * 2,strlen对于汉字的输出是n*3,不是n*2;
*/
$txt1 = '字符串第一部分';
$txt2 = '字符串第二部分';
print $txt1 . $txt2 .'<br>';
echo 'abc 长度',strlen("abc"),";","你好么 长度",strlen("你好么"), "<br>";
echo 'abc b的位置',strpos("abc","b"),";","你好么 好的位置",strpos("你好么","好") ,"<br>";

/*
 * 1,比较运算符中,==和===是两码事,==纯粹的比较,====加上了类型的判断.
 * 2,echo false什么也不输出;
 * 3,isset()如果变量未设置或者为NULL,则返回False,否则True;挺重要的函数啊~;
 * 4,empty()如果变量不存在或其值为""、0、"0"、NULL、、FALSE、array()、var $var; 以及没有任何属性的对象,则返回True,否则False;
 * */
$int100 = 100;
$str100 = '100';
function BoolToStr($A){
   if ($A === true){
       return "True";
   }
   else{
        return "False";
   }
}
echo BoolToStr($int100 == $str100) , ",", BoolToStr($int100 === $str100),"<br>";//true,false
$ussername = true?"0":"hello";
print BoolToStr(isset($ussername)) . '<br>';//True
print BoolToStr(empty($ussername)) . '<br>';//True

/*
 * 1,switch也支持字符串;
 * 2,没有Default也不抱错.
 * */
$favcolor="red";
switch ($favcolor) {
    case "red":
        echo "你喜欢的颜色是红色!";
        break;
    case "blue":
        echo "你喜欢的颜色是蓝色!";
        break;
    case "green":
        echo "你喜欢的颜色是绿色!";
        break;
    default:
        echo "你喜欢的颜色不是 红, 蓝, 或绿色!";
}

/*
 * 1,count能计算出数组长度,这中写法有点LOW,早晚写个全局类来替代它.
 * 2,Count对字符串无效,结果为1;
 * 3,关联数组的类型也是混着都可以.前面是索引,后面是值,所以这类数组只能Foreach遍历;
 * */
echo "Cars数量:",count($cars),'<br>';//4
echo count("aaaaddd"),'<br>';//1
for ($x = 0; $x ';
}
foreach($cars as $x){
    echo $x, '<br>';
}
$ages = array('梅西'=>29, "C罗"=>'31',5=>8);
foreach($ages as $x => $x_value){
    echo "name:",$x,"age:",$x_value,'<br>';
}

echo $ages["C罗"];
?>



<?php ?>

The above introduces [php learning 2] basic grammar exercise 1, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment