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.
Related
This is probably a very simple one to be answered...
I have a piece of code which I need to pull a certain piece of information.
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('XXXX')->toHTML();?>
For this to work I need the XXXX part to pull the result of the following query:
<?php echo $_product->getAttributeText('warranty') ?>
So the output from the above query will then be the information needed to go in to XXXX.
This markup is completely wrong below but should demonstrate the idea I am trying to achieve:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('<?php echo $_product->getAttributeText('warranty') ?>')->toHTML();?>
You just have a redundant PHP opening <?php inside the code. You are already in PHP context so you can do that call directly.
<?php echo
$this->getLayout()->createBlock('cms/block')->setBlockId($_product->getAttributeText('warranty'))->toHTML();?>
However, this is quite complicated and difficult to debug. I would split it in several lines and use variables... remember that you can do it in that context, you are not bound to do everything in one line only :)
Maybe as simple as:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($_product->getAttributeText('warranty'))->toHTML();?>
If not then I would very much like to know what kind of var (array, int, string, double etc) the setBlockId function needs and what $_product->getAttributeText returns.
echo $this->getLayout()->createBlock('cms/block')->setBlockId($_product->getAttributeText('warranty'))->toHTML();?>
<?php
echo $this->getLayout()
->createBlock('cms/block')
->setBlockId($_product->getAttributeText('warranty'))
->toHTML();
?>
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];
I make this PHP script, but Dreamweaver points (as parsing of the code written) that this string is Incorrect.
In any case it works perfectly, but maybe I'm wrong somethings :
for($i=1; $i<=$npagine; $i++) {
?> <a <? if($i==$index) { ?> class="paginas" <? } else { ?> class="pagine" <? } ?> href="index.php?<?=$splitter_zone?>&index=<?=$i?>"><?=$i?></a> <?
}
Any ideas?
The code looks okay. However, your code is very difficult to read - I can almost understand dreamweaver for choking on it :)
Here is a simpler suggestion:
for($i=1; $i<=$npagine; $i++)
{
$class = ($i == $index ? "paginas" : "pagine");
echo "<a class='$class' href='index.php?$splitter_zone&index=$i'>$i</a>";
}
Not sure, but you are using the short tag for php. And it's not supported on all installs like it use to be. short_open_tag must be on to use short tags.
<?php
// bla
?>
not
<?
//bla
?>
The first thing that's wrong with it is that you should be using <?php to open your PHP tags, not just <?. The short form has been deprecated, and may not always work correctly.
Additionally, the short form <?= to print the output is deprecated. You should be using <?php print or <?php echo. (Yes, I know it makes the code longer... don't grumble about it! ;-))
This is probably what's breaking your program. New installations of PHP will choke on the short form PHP tags. Its as simple as that.
But while I'm here... The second problem you have is the horrible mixing in and out of PHP to and from HTML. You should clean up your code so that you don't have to have so many small bits of PHP. Doing it this way makes it virtually impossible to keep track of your tags in HTML and your braces in PHP. Almost guaranteed to lead to errors, and very difficult to read when you come back to it two years down the line to make a bug fix.
To solve this, I would suggest writing your code more like this:
<?php
for($i=1; $i<=$npagine; $i++) {
if($i==$index) {$class='paginas';} else {$class='pagine';}
.... //output your HTML here, without the if() condition embedded in it.
}
?>
You could simplify that even further using a ternary operator.
Switching to the long-form PHP tags <?php actively discourages excessive switching between PHP and HTML in this way, so you may want to take the opportunity to re-write your code in a more readable form.
In a case like this, there's nothing wrong with using print or echo to output the whole of the HTML tag, rather than switching to HTML mode to print it.
So you could end up with code like this:
<?php
for($i=1; $i<=$npagine; $i++) {
$class = ($index == $i) ? 'paginas' : 'pagine';
print "<a class='{$class}' href='index.php?{$splitter_zone}&index={$i}>{$i}</a>";
}
?>
Much simpler and easier to read, I'm sure you'll agree.
One final point I'd make is that I always advise to avoid using single-character variable names like $i. Try to use something more descriptive to what you're using it for. It seems harmless enough, but imagine trying to search a large program for $i to find a bug. You'd get a lot of false hits.
The PHP and HTML is fine (fsvo. "fine"; for one thing, turn off smart-tags!). Dreamweaver doesn't know how to highlight it properly.
for($i=1; $i<=$npagine; $i++) {
$class=$i==$index?"paginas":"pagine";
echo"<a class='{$class}' href='index.php?{$splitter_zone}&index={$i}>{$i}</a>\r\n";
}
I barely know how to use PHP and I can't seem to make my code show an image if a condition proves true. This is the code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
}
?>
I tried writing this thinking it would work and it didn't:
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
Also I tried a code I found which started with the function: header() but it caused a tremendously long error, which said something like header already defined.
Thanks
You have used 'double quotes' incorrectly in the echo statement.
Try the following:
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg' alt='Preview not available' />"
Regards,
Mahendra Liya.
You should var_dump($first) to know what it contains
check if the condition is really getting true
and also put single quote inside the double quote.
if ($first=="#"){
echo 'yes it is true';
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
}
close the img tag
The part of the query string starting with # (so-called "hash") is not being sent to the server. That is, if your page is called like myblog.com/foo?bar=baz#quux, you php script will only receive myblog.com/foo?bar=baz. You need javascript if you want to handle urls with hashes.
Is there a better way to output data to html page with PHP?
If I like to make a div with some var in php, I will write something like that
print ('<div>'.$var.'</div>');
or
echo "'<div>'.$var.'</div>'";
What is the proper way to do that?
Or a better way, fill a $tempvar and print it once? like that:
$tempvar = '<div>'.$var.'</div>'
print ($tempvar);
In fact, in real life, the var will be fill with much more!
There are 2 differences between echo and print in PHP:
print returns a value. It always returns 1.
echo can take a comma delimited list of arguments to output.
Always returning 1 doesn't seem particularly useful. And a comma delimited list of arguments can be simulated with multiple calls or string concatenation. So the choice between echo and print pretty much comes down to style. Most PHP code that I've seen uses echo.
printf() is a direct analog of c's printf(). If you're comfortable in the c idiom, you might use printf(). A lot of people in the younger generation though, find printf()'s special character syntax to be less readable than the equivalent echo code.
There are probably differences in performance between echo, print and printf, but I wouldn't get too hung up on them since in a database driven web application (PHP's typical domain), printing strings to the client is almost certainly not your bottleneck. The bottom line is that any of the 3 will get the job done and one is not better than another. It's just a matter of style.
you can even write
$var = "hello";
echo "Some Text $var some other text";
// output:
// Some Text hello some other text
or
print("Some Text $var some other text");
// output:
// Some Text hello some other text
doesn't make big difference. This works with double-quotes only. With single quotes it doesn't. example:
$var = "hello";
echo 'Some Text $var some other text'; // Note the single quotes!
// output:
// Some Text $var some other text
or
print('Some Text $var some other text'); // Note the single quotes!
// output:
// Some Text $var some other text
Just try this you gonna love the well formated amount of infos :
<?php
echo '<pre>';
var_dump($your_var);
echo '</pre>';
?>
OK, I explain : set a "code" html format and var_dump show the value, the type, the params ... of the variable.
http://us2.php.net/echo
<div><? print($var); ?></div>
Or if you don't have short tags on, you might need to
<div><?php print($var); ?></div>
if you have the short_open_tag option enabled you can do
<?=$var?>
But some find that messy.
You can also use the following syntax:
echo <<<ENDOFTEXT
<div>
$var
</div>
ENDOFTEXT;
Just make sure the ENDOFTEXT is not indented.
You could do something like this:
<div><?php echo $var; ?></div>
One of the nice things about PHP is that you can insert it into regular HTML, and accomplish things like the above with ease. I've always used echo myself in PHP. Not sure if it's the "proper" way, but it's the easiest.
While echo and print are almost equal, you a using different values. Your first value will result in
<div><value of $var><div>
while the second will result in
'<div>'.<value of $var>.'<div>'
But the rest is semantically almost equal. Since echo and print are no real functions but special language constructs, the parenthesis in your first example is just wrapping the single string value and not the parameter list.
See also https://stackoverflow.com/questions/1462581#1462636 and https://stackoverflow.com/questions/1163473#1163793.
I have read somewhere that echo is faster that print. But its just too small of a performance gain.