user2165184
user2165184

Reputation: 3

how to call a function inside a function

how do i call a function inside a function?

here's my function.php

function query_user() {

$q_user = pg_query("SELECT * FROM users WHERE username='".$_SESSION['username']."' AND     password='".$_SESSION['password']."'");

$r_user = pg_fetch_array($q_user, NULL, PGSQL_ASSOC);
$user_tblrows = pg_num_rows($q_user);

if ($user_tblrows==1) {
     $_SESSION['firstname'] = $r_user['firstname'];
     $_SESSION['lastname'] = $r_user['lastname'];

     function welcome_user() {
        echo $_SESSION['lastname'].', '.$_SESSION['firstname'];
    }
 }

in my seperate file i want to call the function welcome_user()

how would i do that? i am confused. i did this and i know it is not correct.

require 'function.php';
welcome_user();

Upvotes: 0

Views: 123

Answers (2)

rcpayan
rcpayan

Reputation: 535

you can call it like this;

query_user().welcome_user();

Upvotes: 0

Kasyx
Kasyx

Reputation: 3200

You have to declare your function earlier, but not inside another function. Then just call it inside:

function welcome_user() {
    echo $_SESSION['lastname'].', '.$_SESSION['firstname'];
}

function query_user() {

$q_user = pg_query("SELECT * FROM users WHERE username='".$_SESSION['username']."' AND     password='".$_SESSION['password']."'");

$r_user = pg_fetch_array($q_user, NULL, PGSQL_ASSOC);
$user_tblrows = pg_num_rows($q_user);

if ($user_tblrows==1) {
     $_SESSION['firstname'] = $r_user['firstname'];
     $_SESSION['lastname'] = $r_user['lastname'];

     welcome_user(); //function call, not definition
 }
}

Upvotes: 2

Related Questions