1

Let's say I have a function in php like this:

<?php function hello() {
   $x = 2;
}

and I want to use that variable outside of this function without using global, since I've heard it's a bad idea because of it's security issues. (right?).

Anyway, what is the best way to get this value out? I would be able to do this in java by using "this". But not sure how that works in PHP. Thanks.

Update:

This is the code I needed help with:

<?php
require('includes/connect.php');

function connectHouseObjects() {

        global $mysqli;
        /* Register a prepared statement */
        if ($stmt = $mysqli->prepare('SELECT x FROM house_room1 WHERE user_id = ?')) {

                /* Bind parametres */
                $stmt->bind_param('i', $user_id);

                /* Insert the parameter values */
                $user_id = 1;

                /* Execute the query */
                $stmt->execute();

                /* Bind resultatet */
                $stmt->bind_result($x);

                while ($stmt->fetch()) {
                    //looping through the x's and y's
                }

                /* Close statement */
                $stmt->close();
        return $x;
            } else {
                /* Something went wrong */
                echo 'Something went terrible wrong'     . $mysqli->error;
            }
        }
?>
3
  • Just return the variable from the function. Commented Feb 19, 2014 at 22:10
  • @JohnConde so taking the example above, would I need to return it there? and what if I have several values? Commented Feb 19, 2014 at 22:12
  • return $x. $x can be an array which holds multiple values. Commented Feb 19, 2014 at 22:15

5 Answers 5

5

John Conde is right but to show you the code it would be

<?php 

function hello() {
   $x = 2;
   return $x;
}

$var = hello();
echo $var;

?>
Sign up to request clarification or add additional context in comments.

Comments

5

You can simply return your data:

function hello() {
   $x = 2;
   return $x;
}

and use its result wherever you want;

$x=hello();

Comments

4

Take this for example:

<?php 

function hello(){
    $x=2;
    //this is where you get it out
    return $x;
}
//here we are outside the function
$x = hello();
//$x now equals 2;
?>

Returning the variable from the function allows you to call the function and assign it outside.

Going more object oriented:

<?php

class Talk
{
    protected $message;

    public function setMessage($message){
        //this will set your class variable to what ever is in $message
        $this->message = $message;
    }
    public function getMessage()
    {
        //This will give you what ever your current message is
        return $this->message;
    }
}
//Then to use this you could do
$talk = new Talk();
//That sets up $talk to be an instance of the talk class

$talk->setMessage('Hello');
//This will then sets the message in talk to hello then to retrieve it just do

$message = $talk->getMessage();

//Now outside the class we have a variable $message that contains 'Hello'

Comments

0

You may also create a class, just like in Java, and use a class variable that can be accessed from other functions in that class.

Comments

0

You can simply return the value outside the function:

<?php
function hello() {
    $x = 2;
    return $x;
}

$x = hello();

If you want to return more than one value, you can use the list() function, while returning values as an array:

<?php
function hello() {
    $x = 2;
    $y = 3;
    return array($x, $y);
}

list($x, $y) = hello();

You can find more information about the list() in the PHP manual

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.