Robbie
Robbie

Reputation: 620

Cannot call PHP function even though it is "included"

I've started using PHP lately... all is good except one thing. I am trying to call a function from another php file... but it's not working. It's probably really simple, but I haven't found anything useful to solve it.

I've used "required_once " but it still does not work. Does anyone know where I'm going wrong?

<?php
require_once "/Applications/MAMP/htdocs/me/database_functions.php";
require_once "/Applications/MAMP/htdocs/me/encode_decode.php";

if (isset($_POST['url']) && $_POST['url'] != "http://")
{
//Get the url posted
$long_url = $_POST['url'];

//Create record in long_url table and return it's id
$long_id = create_long_url($long_url);

Everything works so far.. But the problem is this next function call.. it doesn't even go into the function.

$short_url = $encode($long_id);


}...............etc...

encode_decode.php looks a bit like this...

<?php //encode_decode.php


function encode($number)
{
echo "<br />in encode";
//Encode numer to 6 char 
$s = strtr(rtrim(base64_encode(pack('i', $number)), '='), '+/', '-_');

echo $s;

return $s;
}

Any help is greatly appreciated...

Upvotes: 0

Views: 1202

Answers (4)

Steve H
Steve H

Reputation: 561

As all others have said:

$short_url = encode($long_id);

But also you could clean up your require_once statements:

define('DS', DIRECTORY_SEPARATOR);
require_once(dirname(__FILE__) . DS . 'database_functions.php');
require_once(dirname(__FILE__) . DS . 'encode_decode.php');

Instead of the define() and reference to DS you could of course just prefix your file names with '/'. This also assumes your files are relative (but if not just prefix the folder to the filename) - this would make sure you don't get any problems if you move your site from different servers (i.e., testing, production).

Upvotes: 0

Lee
Lee

Reputation: 10603

remove the dollar sign from in front of the function. a dollar sign in PHP indicates a variable

Upvotes: 1

jeffjenx
jeffjenx

Reputation: 17457

You don't need the $ before your function call

$short_url = $encode($long_id);

should be

$short_url = encode($long_id);

Upvotes: 3

Niko
Niko

Reputation: 26730

The dollar sign would only be needed if the function is stored in a variable (which it isn't).

$short_url = encode($long_id);

Upvotes: 1

Related Questions