Jump to content

php define function


djp1988
 Share

Recommended Posts

I use this code:

<?php define ('TITLE', 'My Page Title'); define ('META','a,s,d'); /* <<-- error */include('header.php'); ?>

I know i can define the TITLE to what ever the page title is seen as the header and the <title> html tags are out of reach, but how can i get the PHP to define my <meta="keywords"> and <meta="description"> ?

Link to comment
Share on other sites

My best advice would be to not use the define portion in this case. It would me much easier for you to just use variables, as they can be altered once defined(constants, on the other hand, can't be altered once set, thus the term constants).I would do something like this:

$pageTitle = "Title here";$metaKeywords = "bob,joe,nina, tom";include('header.php');

//This is header.php<title> <?=$pageTitle?></title><meta (i dont know meta tags...) name="keywords" value="<?=$metaKeywords?>"/>

Link to comment
Share on other sites

well i 'define' them because in my header.php there are the site's key words and title, if i ever forget to set a title for a page that includes header.php then the header "main" title and keywords are used, just to be 100% sure not to loose any information and SEO opportunities, my header.php <title> is as show:

<title><?php if (defined('TITLE')) {print TITLE;}else{print'ActionScript Coding'; } ?></title>

Link to comment
Share on other sites

In fact the variable method doesn't work because the header is really everything from the start of the code until the variable page, and so when i make the variable in a page, that is actually loaded after the if statement to print the keywords, I can remember there was a function to force the document to fully load before processing the HTML therefor allowing my variable to be placed further down the page, but what was that php code?

Link to comment
Share on other sites

There's no function like that, there's buffering but that's not going to do what you want. You should include the content page with variables defined on it before writing out the header, or have the content page include the header after defining variables. That's what most applications do.

Link to comment
Share on other sites

Why not just stick the meta tags into the header file? The meta tags are probably going to be exactly the same for every page, so there's really no need to have them in the php script. Then for the title, do what Jhecht suggested.Then in each page, define what the title of the page is before anything else, then include the header etc. You could probably even work in the conditional statement to have the script in header.php check to see if the variable $title exists and is set, and if not, use the default title.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...