Php variable not working

I have in the common header of my php website files (header.inc) this code:

if($pubblicato != '') {echo "<p class='pubblicato'>pubblicato su <cite>$pubblicato</cite></p>

And in a single php file:

<?php
$keywords="Claudel,Annuncio a Maria,Scarpina di raso";
$title="Paul Claudel";
$autore="Daniela Fabiani e Agostino Gentili";
$pubblicato="$Tracce (1981/10)";
include "normal.inc";
$img="$root/multimedia/img/ritratti/Claudel.jpg";
$imgalt="ritratto di Claudel";
include "$root/header.inc"; 
?>

the variable $Tracce is defined in a file called in header.inc, and if I write in the body of the file

<?php echo $Tracce ?>

I can see the expected result.

But the value of $Tracce doesn’t appear in his place (as in the above first code): html is

<p class='pubblicato'>pubblicato su <cite> (1981/10)</cite></p>

Where I’m wrong?
Thank you!

You have included your header.inc file after referencing $Tracce so it will be undefined at that point.

1 Like

Uhm… Thank you, but I don’t understand.
The file (definiz.php) where $Tracce is definied is included in header.inc (at its beginning).

EDIT

Even this code doesn’t work


<?php
$keywords="Claudel,Annuncio a Maria,Scarpina di raso";
$title="Paul Claudel";
$autore="Daniela Fabiani e Agostino Gentili";
include "$root/definiz.php";
$pubblicato="$Tracce (1981/10)";
include "normal.inc";
$img="$root/multimedia/img/ritratti/Claudel.jpg";
$imgalt="ritratto di Claudel";
include "$root/header.inc"; 
?>

neither this:

<?php
$keywords="Claudel,Annuncio a Maria,Scarpina di raso";
$title="Paul Claudel";
$autore="Daniela Fabiani e Agostino Gentili";
$pubblicato="$Tracce (1981/10)";
include "$root/definiz.php";
include "normal.inc";
$img="$root/multimedia/img/ritratti/Claudel.jpg";
$imgalt="ritratto di Claudel";
include "$root/header.inc"; 
?>

I found the working code:

<?php
$keywords="Claudel,Annuncio a Maria,Scarpina di raso";
$title="Paul Claudel";
$autore="Daniela Fabiani e Agostino Gentili";
include "normal.inc";
include "$root/definiz.php";
$pubblicato="$Tracce (1981/10)";
$img="$root/multimedia/img/ritratti/Claudel.jpg";
$imgalt="ritratto di Claudel";
include "$root/header.inc"; 
?>

Because $root is definied in normal.inc :slight_smile:

But there would not be a simpler way? In this way I have to add a new row of code for every file with $Tracce. Quite annoying…

Is it possible to see your whole project anywhere? (Can you put it on Github?)

Maybe I am completely wrong but I don’t understand your way of programming.

For example: I never ever user include in the middle of a code. I include all needed files at the top of each source code.
I never ever start a variable name with an uppercase capital.
If I need $root and $tracce in all files, I make one config file which is included as first file on top of each other file. So I can always access it.

1 Like

When you find yourself doing something repetitive in programming, it is a sign that you are doing something wrong and you need to rethink the structure.

2 Likes

I dont necessarily think its doing something “wrong”, but its definitely something that can be simplified. Often times a first pass through a coding problem is literally translating your solution steps into code to make sure the logic is sound and gets the correct outcome, and then taking a second pass to clean up.

1 Like

I would prefer contact you privately. Thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.