Если ваш src находится в разметке, он будет извлечен до выполнения вашего js. Просто не используйте атрибут src
, введите src, скажем, data-src
, затем возьмите его с помощью js и сделайте то, что вы хотите.
Рекомендуется использовать isset
по следующим причинам:
$ _ POST ['var']
- пустая строка или «0»
, isset
все равно обнаружит, что переменная существует. Просто повторяя то, что говорили другие, если вы выполните:
if($variable)
и переменная $ не установлена , вы получите сообщение об ошибке. Плюс ..
$var = 0;
if($variable) {
//This code will never run, because $var is false
}
, но использование isset в этом случае вернет true.
They aren't the same. Consider a notional array:
$arr = array(
'a' => false,
'b' => 0,
'c' => '',
'd' => array(),
'e' => null,
'f' => 0.0,
);
Assuming $x
is one of those keys ('a' to 'f') and the key 'g' which isn't there it works like this:
$arr[$x]
is false
for all keys a to g;isset($arr[$x])
is true
for keys a, b, c, d and f but false
for e and g; andarray_key_exists($x, $arr)
is true
for all keys a to f, false
for g.I suggest you look at PHP's type juggling, specifically conversion to booleans.
Lastly, what you're doing is called micro-optimization. Never choose which one of those by whichever is perceived to be faster. Whichever is faster is so negligible in difference that it should never be a factor even if you could reliably determine which is faster (which I'm not sure you could to any statistically significant level).
В строгом PHP вам необходимо проверить, установлена ли переменная перед ее использованием.
error_reporting(E_ALL | E_STRICT);
Что вы здесь делаете
if ($ var)
Не проверяет, установлено ли значение. Таким образом, Strict PHP генерирует уведомление о неустановленных переменных. (это часто случается с массивами)
Также в строгом PHP (просто для справки для вас или других) использование unset var в качестве аргумента в функции вызовет уведомление, и вы не сможете проверить isset () внутри функция, чтобы этого избежать.
isset
tests that the variable has any value, while the if tests the value of the variable.
For example:
// $_POST['var'] == 'false' (the string false)
if (isset($_POST['var'])) {
// Will enter this if
}
if ($_POST['var']) {
// Won't enter this one
}
The big problem is that the equivalency of the two expressions depends on the value of the variable you are checking, so you can't make assumptions.