开发者

Is it possible to write theses 2 lines in to one line? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_如何学JAVA

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

Improve this question
$fail_row = mysql_fetch_array($sql_fail_check);
$tries = $time_row['tries'];

I was reading that it saves memory to do thing inline/less lines. Since i am only going to need the tries count from that MySql array only, could i make this be just one line somehow?


Forget that piece of advice - it is utter, complete nonsense.

Re your update: The advice you quote is suggesting compressing

$description = strip_tags($_POST['description']);
echo $description;

into

echo strip_tags($_POST['description']);

this makes sense for very large amounts of data, because the step of storing the value in a variable ($description) doubles the amount of memory needed to store description, which could lead memory limit problems.

In your case however,

  • There is no elegant way to directly address an element from an array coming from a function call (like $tries = mysql_fetch_array($sql_fail_check)["tries"];) - it's syntactically not possible.

  • tries is probably never going to be large enough for this optimization to make any difference.

Remember: Optimize only when necessary, or if you know that you are going to be dealign with really significant amounts of data. Unless this is the case, code readability always comes first.


Actually , if i understand this right, then: doing $a = 1; $b = $a; will not use twice as much memory, because it will just mean that $a and $b both point to same address in memory.

Then new space is allocated only if you now change either $a or $b.


You could do this:

$tries = mysql_fetch_object($sql_fail_check)->tries;


Yes, you can combine both lines. Given tries is the first SELECTed column you could for example do:

list($tries) = mysql_fetch_num($result);

See this answer for more info on Array Dereferencing (this is what they call what you're trying to do).

PS: Just another reason to use PDO, where you just write:

$tries = $pdo->query('SELECT tries FROM table')->fetchColumn(0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜