Jump to content

Functions - Returning a variable.


ataria

Recommended Posts

Okay.

What I am looking to do is run a variable into a function ( function($variable); ) and then, have it at the end, predefine it in the function.

 

so, the script would look likeeee.

$variable = $row[variable];
function($variable);

and, then I can use it on the rest of the script as $variable.

 

is that possible, If so, how do i do it?

 

Link to comment
https://forums.phpfreaks.com/topic/49900-functions-returning-a-variable/
Share on other sites

Okay.

I'll just make a quick little code snippet.

 

<?php
// would be the function.

function secure_int($variable){
$variable = intval($variable);
return $variable;
}

// would be the script.

secure_int($id);
echo "Your ID is ".$id."!";
?>

 

 

see how I didn't use "$id = function($id);"

 

is that possible? =/

<?php
// would be the function.

function secure_int($variable)
{
return intval($variable);
}

// would be the script.

$id = secure_int($id);
echo "Your ID is ".$id."!";
?>

 

the amazing thing here is that you ave managed to generate a function that does the exact same job as an already avialable built in function AND given it a longer name.

 

if you look at the code you could just do this and get exactly teh same result with a little time saving...

<?php
echo "Your ID is ". intval($id) ."!";
?>

 

or (if you use $id more in the script)

<?php
$id = intval($id);
echo "Your ID is ". $id ."!";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.