Syntax help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Syntax help

Post by markosjal »

I currently have calls to a translation table that look like this

Code: Select all

<?php echo $lang['ACCOUNT_LINK']; ?>
They are working just fine

I have added a number of items to the translation tables. Those additions are items that are already variables that the system currently calls like this

Code: Select all

<?php echo $rowsubcat['subcatname']; ?

Now I do not know the proper syntax to make this work but If I could say I need something like

Code: Select all

<?php echo $lang($rowsubcat['subcatname']); ?>
I want the result of $rowsubcat['subcatname'] to go through the translation table ($lang)


I see however that this may not be correct, but not sure I have not made an error elsewhere. If anyone underrstands what I am trying to do please let me know what is the correct syntax, if this is incorrct.

Any help appreciated.


Thanks

Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Syntax help

Post by Weirdan »

Code: Select all

<?php echo $lang[$rowsubcat['subcatname']]; ?>
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: Syntax help

Post by markosjal »

Yes that worked very well, biut only in the first part I tested. I have a hard time when it comes to the bracketing in PHP!
Some things I will never understand.

I have encontered two parts of the script where I thought I could use the same technique however it does not work

current code

Code: Select all

<a href="{$catlink}">$xcatname</a>

// I tried 
<a href="{$catlink}">$lang['$xcatname']</a>

and this does not work

In another place the current code is

Code: Select all

<?php echo "$row[catname] $path_sep $row[subcatname]"; ?>

and I tried 

<?php echo "$lang[$row['catname']] $path_sep $lang[$row['subcatname]]"; ?>
and this does not work either.

I tried double quotes, single quotes and no quotes.

Open to suggestions



Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Syntax help

Post by Weirdan »

Code: Select all

$lang['$xcatname']
doesn't work because you used single quotes. Variables in single quotes are not interpreted. You need to use $lang[$xcatname] here

Code: Select all

<?php echo "$lang[$row['catname']] $path_sep $lang[$row['subcatname]]"; ?>
Here you could either concatenate your variables into string (instead of interpolating) :

Code: Select all

<?php echo $lang[$row['catname']] . " " . $path_sep . " " . $lang[$row['subcatname']]; ?>
or use curly brackets for interpolation (so-called 'complex' syntax):

Code: Select all

<?php echo "{$lang[$row['catname']]} {$path_sep} {$lang[$row['subcatname']]}"; ?>
Post Reply