Home Backend Development PHP Tutorial what are variables from outside php

what are variables from outside php

Jun 20, 2017 am 10:06 AM
php variable

HTML Forms (GET and POST)

When a form is submitted to a PHP script, the information in the form is automatically available in the script. There are many ways to access this information, for example:

Example #1 A simple HTML form

<form action="foo.php" method="POST">
    Name:  <input type="text" name="username"><br />
    Email: <input type="text" name="email"><br />
    <input type="submit" name="submit" value="Submit me!" />
</form>
Copy after login

There are many ways to access data in an HTML form, depending on your specific setup and personal preference. For example:

Example #2 Accessing data from a simple POST HTML form

<?php
// 自 PHP 4.1.0 起可用
   echo $_POST[&#39;username&#39;];
   echo $_REQUEST[&#39;username&#39;];
   
   import_request_variables(&#39;p&#39;, &#39;p_&#39;);
   echo $p_username;// 自 PHP 5.0.0 起,这些长格式的预定义变量
// 可用 register_long_arrays 指令关闭。   echo $HTTP_POST_VARS[&#39;username&#39;];// 如果 PHP 指令 register_globals = on 时可用。不过自
// PHP 4.2.0 起默认值为 register_globals = off。
// 不提倡使用/依赖此种方法。   echo $username;
?>
Copy after login

Using a GET form is similar, but with the appropriate GET predefined variables. GET also works with QUERY_STRING (the information after the "?" in the URL). So, for example, http://www.example.com/test.php?id=3 contains GET data that can be accessed with $_GET['id'] . See $_REQUEST and import_request_variables() .

Note:

Superglobal arrays such as $_POST and $_GET are available since PHP 4.1.0.

Note:

Dots and spaces in variable names are converted to underscores. For example, becomes $_REQUEST["a_b"].

As shown above, the default value of register_globals before PHP 4.2.0 is on. The PHP community encourages everyone not to rely on this directive and recommends coding assuming it is off.

Note:

magic_quotes_gpc configuration directive affects the values ​​of Get, Post and Cookie. If on, the value (It's "PHP!") is automatically converted to (It\'s \"PHP!\"). Over a decade ago database inserts required such escaping, which is now obsolete and should be turned off. See addslashes() , stripslashes() and magic_quotes_sybase.

PHP also understands arrays in the context of form variables (see related FAQ). For example, you can group related variables, or use this feature to get values ​​from a multi-select input box. For example, POST a form to yourself and display the data when submitted:

Example #3 More complex form variables

<?php
if (isset( $_POST [ &#39;action&#39; ]) &&  $_POST [ &#39;action&#39; ] ==  &#39;submitted&#39; ) {
    echo  &#39;<pre class="brush:php;toolbar:false">&#39; ;

     print_r ( $_POST );
    echo  &#39;<a href="&#39; .  $_SERVER [ &#39;PHP_SELF&#39; ] . &#39;">Please try again</a>&#39; ;

    echo  &#39;
' ; } else { ?>
Name:
Email:
Beer:

Copy after login

IMAGE SUBMIT variable name

When the form is submitted , you can use an image instead of the standard submit button, with a tag like this:

<input type="image" src="image.gif" name="sub" />
Copy after login

When the user clicks somewhere in the image, the corresponding form will be sent to the server, and two Variables sub_x and sub_y. They contain the coordinates of the image the user clicked on. Experienced users may notice that the actual variable name sent by the browser contains a dot instead of an underscore (i.e. sub.x and sub.y), but PHP automatically converts the dot to an underscore.

HTTP Cookies

PHP transparently supports HTTP cookies as defined in » RFC 6265. Cookies are a mechanism that stores data on a remote browser and can track or identify users who visit again. Cookies can be set using the setcookie() function. Cookies are part of the HTTP headers, so the SetCookie function must be called before any output is sent to the browser. The same restrictions apply to the header() function. Cookie data will be available in the corresponding cookie data array, such as $_COOKIE , $HTTP_COOKIE_VARS and $_REQUEST . See the setcookie() man page for more details and examples.

If you want to assign multiple values ​​​​to a cookie variable, you must assign it to an array. For example:

<?php
  setcookie ( "MyCookie[foo]" ,  &#39;Testing 1&#39; ,  time ()+ 3600 );
   setcookie ( "MyCookie[bar]" ,  &#39;Testing 2&#39; ,  time ()+ 3600 );
?>
Copy after login

This will create two separate cookies, even though MyCookie is a single array in the script. If you want to set multiple values ​​in just one cookie, consider using serialize() or explode() on the values ​​first.

Note that a cookie will replace the previous cookie with the same name in the browser, unless the path or domain is different. Therefore, the shopping cart program can retain a counter and pass it together, for example:

Example #4 An example of setcookie()

<?php
if (isset( $_COOKIE [ &#39;count&#39; ])) {
     $count  =  $_COOKIE [ &#39;count&#39; ] +  1 ;
} else {
     $count  =  1 ;
}
setcookie ( &#39;count&#39; ,  $count ,  time ()+ 3600 );
setcookie ( "Cart[ $count ]" ,  $item ,  time ()+ 3600 );
?>
Copy after login

变量名中的点

通常,PHP 不会改变传递给脚本中的变量名。然而应该注意到点(句号)不是 PHP 变量名中的合法字符。至于原因,看看:

<?php
$varname . ext ;   
?>
Copy after login

这时,解析器看到是一个名为 $varname 的变量,后面跟着一个字符串连接运算符,后面跟着一个裸字符串(即没有加引号的字符串,且不匹配任何已知的健名或保留字)'ext'。很明显这不是想要的结果。

出于此原因,要注意 PHP 将会自动将变量名中的点替换成下划线。

确定变量类型

因为 PHP 会判断变量类型并在需要时进行转换(通常情况下),因此在某一时刻给定的变量是何种类型并不明显。PHP 包括几个函数可以判断变量的类型,例如: gettype() , is_array() , is_float() , is_int() , is_object()和 is_string() 

The above is the detailed content of what are variables from outside php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles