Home > Backend Development > PHP Tutorial > Some basic tags for PHP learning

Some basic tags for PHP learning

不言
Release: 2023-03-25 09:22:01
Original
4974 people have browsed it

This article mainly introduces some basic tags about PHP learning, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

The $ symbol in 1.php is a variable symbol; add the $ symbol to a string, and the string is a variable name or object name.

2.echo is the output symbol

The meaning of directly outputting characters or strings: For example: echo "abc"; will output abc. echo 'abc' will also output abc. If only a string is output, the output content of single quotes and double quotes is the same. If you want to output a string variable, for example, the string variable $a='abc'; echo "$a123" will output abc123, but if you use echo '$a123', only $a123 will be output, that is to say, the value in single quotes The content will be output as is, and the double quotes will determine whether there are variables in it. If there are variables, they will be converted into the values ​​of the variables.

3.What is COOKIE?

Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.

How to create a cookie?

The setcookie() function is used to set cookies.

Note: setcookie() function must be located before the tag.

Syntax

setcookie(name, value, expire, path, domain);
Copy after login

Example 1

In the following example, we will create a cookie named "user" and assign it the value "runoob". We also specify that this cookie expires after one hour:

<?php
setcookie("user", "runoob", time()+3600);?><html>.....
Copy after login

Note: The cookie value is automatically URL-encoded when sending the cookie and automatically decoded when retrieved. (To prevent URL encoding, use setrawcookie() instead.)

Example 2

You can also set the cookie expiration time in another way. This may be simpler than using seconds.

<?php
$expire=time()+60*60*24*30;setcookie("user", "runoob", $expire);?><html>.....
Copy after login

In the above example, the expiration time is set to one month (60 seconds * 60 minutes * 24 hours * 30 days).


How to retrieve the value of Cookie?

PHP’s $_COOKIE variable is used to retrieve the value of the cookie.

In the following example, we retrieve the value of the cookie named "user" and display it on the page:

<?php// 输出 cookie 值echo $_COOKIE["user"];// 查看所有 cookieprint_r($_COOKIE);?>
Copy after login

In the following example, we use isset() function to confirm whether the cookie has been set:

<html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body><?phpif (isset($_COOKIE["user"]))    echo "欢迎 " . $_COOKIE["user"] . "!<br>";else    echo "普通访客!<br>";?></body></html>
Copy after login



如何删除 Cookie?

当删除 cookie 时,您应当使过期日期变更为过去的时间点。

删除的实例:

<?php// 设置 cookie 过期时间为过去 1 小时setcookie("user", "", time()-3600);?>
Copy after login



如果浏览器不支持 Cookie 该怎么办?

如果您的应用程序需要与不支持 cookie 的浏览器打交道,那么您不得不使用其他的办法在您的应用程序中的页面之间传递信息。一种方式是通过表单传递数据(有关表单和用户输入的内容,在本教程的前面章节中我们已经介绍过了)。

下面的表单在用户单点击 "Submit" 按钮时,向 "welcome.php" 提交了用户输入:

<html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body><form action="welcome.php" method="post">名字: <input type="text" name="name">年龄: <input type="text" name="age"><input type="submit"></form></body></html>
Copy after login

取回 "welcome.php" 文件中的值,如下所示:

<html><head><meta charset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body>欢迎 <?php echo $_POST["name"]; ?>.<br>你 <?php echo $_POST["age"]; ?> 岁了。</body></html>
Copy after login

4. HTML 标签的 href 属性

HTML 标签

实例


href 属性规定链接的目标:

亲自试一试

定义和用法


标签的 href 属性用于指定超链接目标的 URL。

href 属性的值可以是任何有效文档的相对或绝对 URL,包括片段标识符和 JavaScript 代码段。如果用户选择了 标签中的内容,那么浏览器会尝试检索并显示 href 属性指定的 URL 所表示的文档,或者执行 JavaScript 表达式、方法和函数的列表。

提示和注释

注意: 标签中必须提供 href 属性或 name 属性。

制作文本链接

一个引用其他文档的简单 标签可以是下列形式:

浏览器用特殊效果显示短语“W3School 在线教程”(通常是带下划线的蓝色文本),这样用户就会知道它是一个可以链接到其他文档的超链接。就像这样:

W3School 在线教程

用户还可以利用浏览器中的选项来自己指定文本颜色、设置链接前和链接后链接文本的颜色。

提示:可以使用 CSS 伪类向文本超链接添加复杂而多样的样式。

制作图像链接


更复杂的锚还可以包含图像。下面这个 LOGO 是一个图像链接,点击该图像,可以返回 W3school 的首页:

上面的代码会为 W3School 的 LOGO 添加一个返回首页的超链接:

W3School 在线教程

大多数图形浏览器都会在作为锚的一部分的图像周围放置特殊的边框。通过在 标签中把图像的 border 属性设置为 0 可以删除超链接的边框。也可以使用 CSS 的边框属性来全局性地改变元素的边框样式。

语法

属性值

描述
URL 超链接的 URL。可能的值:
  • 绝对 URL - 指向另一个站点(比如 href="http://www.example.com/index.htm")

  • 相对 URL - 指向站点内的某个文件(href="index.htm")

  • 锚 URL - 指向页面中的锚(href="#top")


TIY Example

  • Create a hyperlink

  • This example demonstrates how to create a link in an HTML document.

  • Using images as links

  • This example demonstrates how to use images as links.

HTML tag

5. What does the question mark in php mean?

Question mark in php code The functions of are roughly divided into two categories. One is the pair of tags "" used in PHP tags, and the other is the "?" operator used in the ternary operator. , like this: $a == 1?true:false.

  1. The question mark used in the tag only acts as a delimiter, that is, to identify the beginning and end of the PHP code, and has no substantial program meaning.

  2. The question mark in the ternary operator has a specific program meaning. Its expression method is probably: Condition 1? Condition 2: Condition 3, when condition 1 is true , the function of the question mark is to select condition 2 as the branch to continue execution of the program. That is to say, once the condition is established, condition two will be executed, otherwise condition three will be executed.

Related recommendations:

PHP Study Notes Post Upload Notes


The above is the detailed content of Some basic tags for PHP learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template