Jump to content

[SOLVED] using strings and variables


Lamez

Recommended Posts

alright, cuz I am lazy, and if one day I decided to change the title, I do not want to change tons of pages so I wanted started while I was ahead.

 

I have a file, like most webmasters, called head.php

 

in head it contains, well my header, and a little php snippet that says:

 

<title><?php include ("style/include/cons/title.php"); ?></title>

and in title.php:

Lamez's Corner - <?php
if $title = home{
echo "Home";
}
elseif $title = login{
echo "Login";
}
elseif $title = register{
echo "Register";
}
elseif $title = member{
echo "Member's Area";
}
elseif $title = admin{
echo "Admin's Center";
}
?>

 

the reason why I am tell you this is because I want to type a variable in my HTML coding that will give the page a title for example:

 

<?php 
$title == home;
include "style/include/cons/head.php"; 
?>

 

well it did not work out, that is why I am posting here, I get this error:

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /mounted-storage/home48c/sub007/sc33591-LWQU/www/style/include/cons/title.php on line 3

 

line three in title.php is:

if $title = home{

 

I am guessing the main problem is I am not setting the variables right, or not calling the variable right.

 

Any Help?

 

 

Link to comment
https://forums.phpfreaks.com/topic/81774-solved-using-strings-and-variables/
Share on other sites

Setting the variable should be

 

$title = "home";

 

and calling it in the if statement should be

 

if ($title == "home") {

 

Also, your include statement is wrong - the filename should be contained in brackets

 

include "style/include/cons/head.php";

should be

include("style/include/cons/head.php");

you need to do it like this:

 

Lamez's Corner - <?php
if ($title == "home") {
echo "Home";
}
else if ($title == "login") {
echo "Login";
}
else if ($title == "register"){
echo "Register";
}
else if ($title == "member") {
echo "Member's Area";
}
else if ($title == "admin") {
echo "Admin's Center";
}
?>

 

and then you will have to query your "title" variable in your urls:

 

like this:

 

http://www.domain.com/page1.php?title=home

no that is not what I am trying to do :(

 

in the title bar I want it to call the variable.

 

up on the title bar (if title equals to home)

 

"Lamez's Corner - Home : Mozilla Firefox"

 

I am not too sure if i was clear enough on my first post.

just declare the variable before the include.

 

<title><?php
$title="home";
include ("style/include/cons/title.php"); 
?></title>

 

and use this for your include:

 

Lamez's Corner - <?php
if ($title == "home") {
echo "Home";
}
else if ($title == "login") {
echo "Login";
}
else if ($title == "register"){
echo "Register";
}
else if ($title == "member") {
echo "Member's Area";
}
else if ($title == "admin") {
echo "Admin's Center";
}
?>

I would use a switch statement instead of the if-elseif:

Lamez's Corner - 
<?php
    switch ($title) {
       case 'home':
       case 'login':
       case 'register':
            echo ucwords($title);
            break;
       case 'member':
            echo "Member's Area";
            break;
       case 'admin':
           echo "Admin's Center";
           break;
       default:
           echo "No Title";
    }
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

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