0

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

1
  • $encode($long_id); why a $ to begin with? Commented Jun 8, 2012 at 12:45

4 Answers 4

3

You don't need the $ before your function call

$short_url = $encode($long_id);

should be

$short_url = encode($long_id);
Sign up to request clarification or add additional context in comments.

Comments

1

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

$short_url = encode($long_id);

Comments

1

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

Comments

0

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).

2 Comments

Great idea, that looks a lot cleaner! Thanks!
Sorry I have edited my answer as I stupidly missed out the directory separator as dirname(FILE) doesn't ordinarily prepend a trailing slash to the path. Using the above code will make it safe whether you're using windows or linux (or better yet have a look at the spl_autoload function: php.net/manual/en/function.spl-autoload.php)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.