PHP variable like $var['$var'] - php

I'm current making a website with a photo album in it. The website in 2 languages, english and dutch. So I made language file like:
$lang['hello'] = 'Hallo'; //Hallo is hello in dutch
With the photo album I'm trying use the same principle like:
$lang['discription_001'] = 'photo of a house';
With showing the images I made a counter, now I want to use the same counter in the dispription like so:
echo $lang['discription_'$counter]
And $counter being 001 for photo number one. However this does not work, Could someone tell how I could get this to work, or any other method to get what I want.
Thanks in advance, Thomas de Zeeuw
P.S. I'm new in PHP, however I normally pick up things quite fast, so make some explaintion would be appreciated.

You're almost there:
echo $lang['discription_' . $counter]
The . is PHP's concatenation operator, for combining strings.

You just forgot the string concatenation operator . to concatenate 'discription_' and the value of $counter:
$lang['discription_'.$counter]

You have a typo.
echo $lang['discription_'$counter]
Should be
echo $lang['discription_' . $counter];

You could get it to work by simple repairing your code:
echo $lang['discription_'.$counter];
or
echo $lang["discription_{$counter}"];

Is $counter a string?? If so your example should work fine if you fix the parse error (You missed the concatenation operator (.).
It would be much better if you showed us actual code from your application.
echo $lang['discription_' . $counter];

As you have got answers already you forgot the concatenation operator, but better way in my mind would be to use multidimensional array.
echo $lang['descriptions'][$counter];

Related

string name out of vlaue of another string php

in PHP I have the following case:
<?php $anz1='3';$i='1';echo $anz.$i;?>
I don't know how to echo $anz1 with the help of $i. I must do it this way, this is just an easy case, I need it for loops where it echos every $anz
Thanks in advance
I hope I could write it understable for you, my english is not the very best.
Maybe this is what you're looking for:
$anz1='3';
$i='1';
echo ${'anz'.$i};
Output:
3

Write two or more instructions in one line with PHP

I have a very simple question for you guys.
After deep researches, I've found nothing useful to get a good answer.
I just really like to know the way to write multiple instructions/commands in the same line, with PHP.
Just to be clear, this:
<?php
if (true) {
echo "First,";
echo " second";
echo " and third.";
}
?>
shoud become this:
<?php
if (true)
echo "First," & echo " second" & echo " and third";
?>
So, the script above can execute three operations in one line of code.
I tried to use the "&" sign to append more instructions in the same line and it seems it works...
Is this the correct way to do what I want to do? May this cause any problems?
Thanks!
PS: the "echo" instruction is just as example (I know that you can merge strings just using the dot (.) sign
PHP puts no significance on a line break at all. All you need to do is remove the line break, everything else can stay exactly the same:
<?php if (true) { echo "First,"; echo " second"; echo " and third."; } ?>
The statements are already terminated and separated by ;.
No. It's not correct. echo is not a function, and is not something you can & together like that. It does, however, support comma-separated "arguments", so something like
echo 'first', 'second', 'third';
is entirely possible and totally valid PHP code.
Even if the & version was possible, you'd actually be LOSING efficiency, because you're doing 3 echo calls, and then trying to combine their non-existence return values. e.g. you'd be turning 3 operations into 5.

Using variable type variables as array names in php

I'm trying to make a dynamic menu in my web, in which only some pages from each section will appear.
The code I wrote was:
$menulist=array();
$menulist[1]='file1%#16';
$menulist[2]='file2%#9';
$menulist[3]='file3%#19';
$menulist[4]='file4%#8';
$menulist[5]='file5%#13';
$menulist[6]='file6%#14';
$menulist[7]='file7%#10';
$menulist[8]='file8%#23';
$menulist[9]='file9%#19';
$menulist[10]='file10%#18';
$menulist[11]='file11%#12';
function actualizaciones($matriz)
{
$linea=explode("%#",$matriz);
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'</li>;
}
echo '<ul>';
array_walk($menulist,'actualizaciones');
echo '</ul>';
Every $linea[0] string is the name of another array (not shown in this code) which contains the text that should be in every possible link corresponding to every key passed by $linea[1].
I must have done something wrong, because the hyperlinks work fine but there's no text showing on them.
use the simple character like below
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'<li>';
and the problem in your code is
.'</li>;
^^^^^
here is the problem it should be
.'</li>';
If I'm reading the question right, you're asking how to use variable variables in PHP.
This can be done using the double-dollar syntax - ie $$linea[0]. See the PHP manual for more info: http://uk.php.net/manual/en/language.variables.variable.php
But if that is what you're doing, I would say you're not writing good code: if variable variables are involved, there's almost always a better way of doing it.
Can't really offer much better assistance here without understanding more about what you're trying to do, but it sounds like you should be using subarrays rather than separate named variables for everything.
Hope that helps.

Displaying an image from a directory in PHP

I'm trying to display an image file from a directory using a PHP echo command and an IMG tag.
Here is the code:
//These variables represent the file name extensions from a form element from a previous page
$bannerimg=$_POST["banimg"];
$adimage=$_POST["adimage"];
echo "<img src='imgdir/'".$bannerimg."/>";
When I echo out the file variables ($bannerimg and $adimage) I get the proper file name and extension.
In theory, will this work? If so, what is the proper syntax to handle that echo statement?
Thanks for all the help.
Dustin
Yes it would work, but you should have tested that already.
Alternative syntaxes to the echo statement that I find a little bit more readable would be:
echo "<img src='imgdir/{$bannerimg}/>";
echo "<img src='imgdir/$bannerimg/>";
You can read all about variable parsing in the manual, the first syntax is the complex one and the second the simple. I prefer the complex one as the end of the variable is clearly defined and you can use it for complex expressions, not just simple variables.
You're doing it right but you can use the following just to keep it clean.
echo "<img src='imgdir/$bannerimg' />";
It should work.
To Sandeep's comment, I would have gone the other way.
echo '<img src="imgdir/'.$bannerimg.'/>';
Using " means the parser needs to check to see if there is anything to evaluate.

How to read this php code?

How to decode this string in php?
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
It looks like a regex URL, but how to read what it is?
Thanks.
just echo it out.
<?
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
echo $x;
It outputs:
https://product-search.api.cj.com/v2/product-search?
Print it!
% cat test.php
#!/usr/bin/env php
<?php
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
print $x;
?>
% ./test.php
https://product-search.api.cj.com/v2/product-search?
It is actually a string with some characters specified in hexadecimal and octal notation. Just echo it.
just print it, its a url
https://product-search.api.cj.com/v2/product-search?
Well, I created a PHP page as follows:
<?php
$x = "h\164tp\163\072\057\057\x70r\157\x64\x75\x63t-\x73\x65a\x72\143\150\056\x61\160i\x2e\x63\x6a\x2ec\x6f\x6d\057v\062/\160\x72\157\x64\165\x63t\x2ds\x65\141\162\143\x68\x3f";
print $x;
?>
and ran it.
And I got the following:
https://product-search.api.cj.com/v2/product-search?
Which means nothing to me, except that cj.com is part of Commission Junction, which is an online advertising network.
It's been deliberately obfuscated, so clearly the person who wrote it intended that you didn't notice it or understand it, and would leave it alone. I don't know the context of the question, why you're asking about it, but my guess would be that you've been hacked and someone has inserted this code (and more) into your site.
If that's the case, their aim would clearly be to gain some advertising revenue by freeloading on your site. Not particularly malicious as hacks go, but not something you'd want to be happening (especially if you don't know what kind of ads would be shown).
Simple:
utf8_decode();
http://www.php.net/manual/en/function.utf8-decode.php

Categories