views:

36

answers:

5

I am trying to migrate my site to PHP and use a WP theme on my site with out modifying the theme too much. So my problem is that I want to create a functions.php file like in Wordpress that contains some code that can be called to load the header and footer of my sites theme from each page. Also I want to keep some constants in another file that will be used in my site for loading files from my sub-domains, getting the current year, etc. I have a basic get_file() function for loading files from my static content domain, But when I try to run it I get this:

Parse error: syntax error, unexpected T_FUNCTION in /home/s0urc3/public_html/includes/functions.php on line 4

Here are the contents of my functions.php and my constants.php:

functions.php

<?php
include('constants.php')
/*Gets a file from the domain http://files01.s0urc3.ismywebsite.com/*/
function get_file($file)
{
FILE_ROOT + $file
}
get_file(images/bg.jpg)
?>

constants.php

<?php
/* CONSTANTS*/
define(FILE_ROOT, "http://files01.s0urc3.ismywebsite.com/")
define(HOME, "http://s0urc3.ismywebsite.com/")
define(BLOG_HOME, "http://blog.s0urc3.ismywebsite.com/")
define(FORUMS_HOME, "http://forums.s0urc3.ismywebsite.com/")
define(YEAR, getdate(year))
/*define(FILE_ROOT, "http://files01.s0urc3.ismywebsite.com/")*/
?>

Any and all help would be much appreciated.

A: 

Your Syntax makes no sense, look what your function does, it would be the same as just writing:

function get_file($file)
{
    "http://files01.s0urc3.ismywebsite.com/" + $file
}

Thats not PHP or anything, what exactly is it you want to do? If you want the complete filepath, it would be :

function get_file($file)
{
    return FILE_ROOT . $file;
}
// returns: http://files01.s0urc3.ismywebsite.com/images/bg.jpg
Hannes
Seems it's a mix of javascript and templating code, which isn't really php code
Viper_Sb
well, everyone starts somewhere ...
Hannes
oops i am more experienced with js html and css than php.
ellisgeek
+1  A: 

In PHP most lines end with a semicolon ';' This is what is giving those errors.

// should be
include('constants.php');
get_file('images/bg.jpg');
// etc.....
Viper_Sb
and the + is also wrong, thats not JS xD
Hannes
+3  A: 

You have virtually no lines free of syntax errors. You will never get anywhere muddling your way forward this way. Find a tutorial and learn PHP before trying to port your code.

Specifically:

In PHP, as in other C-style languages, your statements must end with semicolons:

// bad; missing semi-colon
include('constants.php')
// good:
include('constants.php');

Your uses of define are wrong. The symbol to define must be passed to define() as a string before it can be used as a symbol:

// bad; FILE_ROOT not yet defined, missing semi-colon
define(FILE_ROOT, "http://files01.s0urc3.ismywebsite.com/")
// good:
define('FILE_ROOT', "http://files01.s0urc3.ismywebsite.com/");

Strings must be enclosed in either single quotes or double quotes:

// bad; missing quotes around string, missing semi-colon
get_file(images/bg.jpg)
// good:
get_file('images/bg.jpg');

Values must be explicitly returned from functions via the return keyword, and strings are concatenated using the dot . operator:

// bad; Perform integer addition, doesn't return, no semi-colon
FILE_ROOT + $file
// good:
return FILE_ROOT . $file;

And a note on style: You've commented get_file as 'getting a file', which it definitely doesn't do. All it does is return the fully-qualified URL of the relative URL you pass it. Your function names and comments should strive to clearly tell you what they do. In the case of such a short utility function, I'd go for something very short:

// Return the fully-qualified URL for the given file
function url_for($file)
{
  return FILE_ROOT . $file
}
meagar
Thank you so much... this helped so very much.
ellisgeek
A: 

You should have 2 seperate files, constants and functions

Put data in each accordingly and the simply include them and use

require_once 'constants.php';
require_once 'functions.php';

within functions the example layout should be like so:

function a()
{
    //...
}
function b()
{
    //...
}
function c()
{
    //...
}

followed by usages:

if(!user_session_active())
{
    location('home');
}
get_file('some_file.ext');
RobertPitt
A: 
  1. you forgot semicolons at the end of every line where you have to use one. (after the defines, after the include, after the function call...)
  2. string concatenation in php works bu using ., not +
oezi
Thanks that helped alot!
ellisgeek