Pass A Variable From One Function To Another In PHP

admin_img Posted By Bajarangi soft , Posted On 24-09-2020

You need pass variable by refernce to a function so the function can modify the variable .

Pass-variable-from-one-function-to-another

First Need to Create Function
1.Create Function:

<?php
$abc = 10;
$xyz = 20;

function myTest() {
    $GLOBALS['xyz'] = $GLOBALS['abc'] + $GLOBALS['xyz'];
}

myTest();
echo $xyz; // outputs 30
?>

Complete Code of Sort() Array Elements:
<!DOCTYPE html>
<html lang="en">
<head>
    <title>PHP string replace function</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>How can I pass a variable from one function to another in PHP?</h1>
    </div>
    <br>
    <div class="well">
        <?php
        $abc = 10;
        $xyz = 20;

        function myTest() {
            $GLOBALS['xyz'] = $GLOBALS['abc'] + $GLOBALS['xyz'];
        }

        myTest();
        echo $xyz; // outputs 30
        ?>


    </div>
    <br>
</div>
</body>
</html>

Related Post