search
HomeBackend DevelopmentPHP ProblemWhat are the ways to pass values ​​in php?

There are 4 ways to pass values ​​in PHP, which are: 1. Using the cookie of the client browser; 2. Using the server-side session; 3. Using the form to pass; 4. Using hyperlinks to pass parameters.

What are the ways to pass values ​​in php?

There are four ways to pass values ​​in php

We define page01.php and page02.php PHP file, find a way to transfer the content in page01 to page02, and then let us continue to use it.

The first type:

Use the cookie of the client browser. A cookie is easy to understand. It is a temporary file. You can think of it as a storage room. The browser records some information during the browsing process and temporarily stores it here.

Set a cookie in page01.

The code is as follows:

<?php 
       setcookie(&#39;mycookie&#39;,&#39;自灵&#39;);
?>

We have defined a variable mycookie, whose value is the string 'self-spirit'.

We can name cookie variables whatever we want and define multiple cookie variables.

Accept cookies on page02.

The code is as follows:

<?php
     $wuziling = $_COOKIE[&#39;mycookie&#39;];
     echo $wuziling;
?>

We use $_COOKIE[] to extract the variable mycookie in the cookie and pay its value to $wuziling. Then simply output.

Okay, here we have completed using cookies to transfer parameters from page to page.

Second:

Use server-side session. Understanding session is very easy. The difference from a cookie is that it is a temporary storage on the server side. Session is often called a session.

Set a session in page01.

The code is as follows:

<?php 
session_start();
$_SESSION["temp"]=array(&#39;123&#39;,&#39;456&#39;,&#39;789&#39;);
?>

To use session, session must be started. session_start(); is the method to start the session. Generally it should be written first.

In the second statement, I defined a $_SESSION["temp"] array. The name of the array is $_SESSION["temp"], which stores 3 strings.

Accept session on page02.

The code is as follows:

<?php 
     session_start();
     for($i=0;$i<3;$i++)
     {
             echo $_SESSION[&#39;temp&#39;][$i].&#39;<br />&#39;;
     }
?>

First start the session. After startup, the variables we defined in page01 are already available and do not require any other acquisition operations. This is different from cookies.

Below we use a for loop to output its content.

Don’t think that $_SESSION['temp'][$i] is a two-dimensional array. It is a one-dimensional array. The name of the array is $_SESSION["temp"]. Although this name is more complicated, the array The subscript is 'temp'

When we write $_SESSION["temp"], temp plus double quotes or single quotes are equivalent.

Here we define an array when defining session variables, or we can define ordinary variables, just like what is mentioned in cookies.

Third option:

Use a form to pass.

page01.php is written like this:

The code is as follows:

<form action="page02.php" method="post">
     <input type="text" name="wuziling" />
     <input type="submit" name="submit" value="提交" />
</form>

The attribute action in the form directly specifies which page the content of this form is passed to. method specifies the method of transfer. post stands for using messaging, just like how we send text messages. ,

page02.php is written like this:

The code is as follows:

<?php 
     $wu = $_POST[&#39;wuziling&#39;];
     echo $wu;
?>

Use $_POST[] to get the passed variable value. This variable name wuziling is defined in the name attribute of the form's input tag.

Then pass it to another variable $wu. So we can output. Direct output is also possible, echo $_POST['wuziling'];

Fourth method:

Use hyperlinks to pass parameters. Many of our online operations involve clicking on hyperlinks to jump between web pages. Parameters can also be passed while clicking.

page01.php is written like this:

The code is as follows:

<?php 
$var = &#39;I love you !&#39;;
?>
<a href="<?php echo "page02.php?new=".$var ?>">get</a>

Define a variable $var and specify in the href attribute of hyperlink a that you want to jump to the page02 page. Add a question mark after it, and a self-defined variable new. This name will be used on the page02 page. The value of new is the $var we want to pass.

page02.php is written like this:

The code is as follows:

<?php
     echo   $_GET[&#39;new&#39;];
?>

Use $_GET[] to get the value of new, and then you can output or use it for other purposes. You can directly see the new variable and its value in the address bar of the browser. .

The above is the detailed content of What are the ways to pass values ​​in php?. For more information, please follow other related articles on the PHP Chinese website!

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft