-1

i am trying to have the final output be

$<a style='cursor: pointer; class="photogallery-album-thumbs" onclick=fetchalbum(albumid and albumname)>

here is the code I know it is completely messed up.

$echo '<a style='cursor: pointer; onClick=fetchAlbum(' . $values['aid'] . "\" class='photo-gallery-album-thumbs-title' " . $values['name'] . ")>";

thank you in advance.

  • 2
    If you start with a "`" and you want to put a "`" in, you need to escape it, like after your 'style'. Also, why is there a "$" sign in front of your echo? – Nanne Apr 22 '12 at 07:01
  • you are mixing PHP - JS - HTML. that's bad. Use a js framework + PHP template engine – dynamic Apr 22 '12 at 07:10
  • Just remember, code so humans can read it easily, whether that's you revisiting long down the road, or someone else picking up where you left off. Humans take much, much longer than computers do. – Blake Apr 22 '12 at 07:18

3 Answers3

1

Best thing todo is get a good code editor with syntax highlighting, one built for PHP coding.

But here are some basics:

<?php 
/*Standard Variable--
Anything placed between quotes is treated as a string. 
A string quote must start and end, you can continue a string but that block
must also have a start and end quote.
*/
$variable_name = "Value";
//or
$variable_name = 'Value';

//If you have a line of html with lots of double quotes, its sometime easyier to use single quotes
$variable_name = '<a style="cursor: pointer; onClick=fetchAlbum("'.$values['aid'].'") class="photo-gallery-album-thumbs-title"'.$values['name'].'")>';
//Or you have to escape the quotes or replace them with single
$variable_name = "<a style=\"cursor: pointer; onClick=fetchAlbum(\"".$values['aid']."\") class=\"photo-gallery-album-thumbs-title\"".$values['name']."\")>";
//You can also use curly brackets on double quotes but you cant use them on single
$variable_name = "<a style=\"cursor: pointer; onClick=fetchAlbum(\"{$values['aid']}\") class=\"photo-gallery-album-thumbs-title\"{$values['name']}\")>";
//Also you cant put carriage returns or tabs ect in single quotes
$variable_name = "\tSome value\r\n";
//tho yo can do
$variable_name = "\t".'Some value'.PHP_EOL;
//Using double quotes for variable assignment or printing is slower then single quotes
//Concatenation
$variable_name = "v"."a"."l"."u"."e";
//variable continuing
$variable_name .= "value";

//simple echoing out
echo 'Some value';
echo "Some value";
echo $variable_name;
print "Some value";
//or you can break out of php and put your html
?>
Lawrence Cherone
  • 41,907
  • 7
  • 51
  • 92
  • `Using double quotes for variable assignment or printing is slower then single quotes`: I refer you to [PHP Bench](http://www.phpbench.com) for correcting that fallacy. – Blake Apr 22 '12 at 16:07
  • @Blake [Is there a performance benefit single quote vs double quote in php?](http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php) – Lawrence Cherone Apr 22 '12 at 16:11
  • You referred me to a question, I referred you to a benchmark. Just because the person asking the question marks it as one, doesn't make it true. Show me any benchmarking that refutes the benchmark I've shown you. – Blake Apr 22 '12 at 16:12
  • @Blake The benchmarks does not include a combination of using print and single double/quotes only variable assignment and single quotes echo/print, tho the difference is most likely so miniscule it dont even matter. Tho my answer still stands and is no way wrong. ;p – Lawrence Cherone Apr 22 '12 at 16:25
  • I'm not saying your answer is wrong, just that statement. :) – Blake Apr 22 '12 at 19:26
0

Try this line:

echo '<a style="cursor: pointer;" onClick="fetchAlbum('.$values['aid'].')" class="photo-gallery-album-thumbs-title">'.$values['name'].'</a>';

It's important to have the same count of quotes. They work like brackets like in math.

The dollar sign is not necessary. The $ indicates most likly a variable. A function name must not contain a dolar sign.

From the php documentation:

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

rekire
  • 45,039
  • 29
  • 149
  • 249
0

I think it's better to use double quote variable interpolation, but reference variables with curly brackets {}. So your php is pretty easy to format / read,

ie:

echo "<a style='cursor: pointer;' onClick='fetchAlbum({$values['aid']})' class='photo-gallery-album-thumbs-title' {$values['name']}>";

Curly brackets make it easy to avoid awkward concats (to me).

If the dollar sign is supposed to be in front of this anchor, you need to escape it like:

echo "\$ ... ";

Blake
  • 2,114
  • 1
  • 13
  • 22
  • Your example has some html syntax errors. – rekire Apr 22 '12 at 07:08
  • 1
    Okay, I missed a single quote for the style block. Sue me. :P – Blake Apr 22 '12 at 07:09
  • 1
    I use single quotes when handling html because most html property's values are double quotes. like you took the time to change all in your example – Lawrence Cherone Apr 22 '12 at 07:11
  • Browsers could care less if a property is defined with a single or double quote. See: http://stackoverflow.com/questions/273354/are-single-quotes-allowed-in-html – Blake Apr 22 '12 at 07:11
  • And I code so that it's easiest to read, whether by me or anyone else. My time is much more valuable than the amount of time it takes a computer to parse it. You really think it's easier to read... `echo "My dog's name is " . $dogName . " and once he said to me \"" . $dogSaid . "\"...";` or `echo "My dog's name is {$dogName} and... \"{$dogSaid}\" ...`? – Blake Apr 22 '12 at 07:23