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
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? =/

Link to comment
Share on other sites

<?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 ."!";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.