编程新手无法弄清楚如何格式化php语法


New to programming can't figure out how to format php syntax

我正在尝试让最终输出

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

这是代码我知道它完全搞砸了。

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

提前谢谢你。

最好的办法是获得一个带有语法突出显示的良好代码编辑器,一个为 PHP 编码构建的代码编辑器。

但这里有一些基础知识:

<?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
?>

我认为最好使用双引号变量插值,但引用带有大括号 {} 的变量。所以你的php很容易格式化/阅读,

即:

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

大括号可以轻松避免尴尬的连冠(对我来说)。

如果美元符号应该在这个锚点的前面,你需要像这样转义它:

echo "'$ ... ";

试试这一行:

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

具有相同的报价计数很重要。它们像数学中的括号一样工作。

美元符号不是必需的。$表示很可能是一个变量。函数名称不得包含 dolar 符号。

从 php 文档中:

函数名称遵循与 PHP 中其他标签相同的规则。有效的函数名称以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它将表示为:[a-zA-Z_''x7f-''xff][a-zA-Z0-9_''x7f-''xff]*