Jump to content

Recommended Posts

No.  You're making that WAY harder than it has to be:

$mysql = "Select * from ";
$mysql .= "Person ";
$mysql .= "Where Name like '%$SearchA%'";

 

. is the concatenation operator as opposed to +.  Use . where you would normally use + and you'll be fine.

Just Figured it out, ARHHHH.

 

 

$mysql = " Select * from  ";           

$mysql = $mysql .= " Person ";

$mysql = $mysql .= " Where Name like '%$SearchA%' ";

 

Thanks Anyway

 

Though that technically works, I thought I'd tell you why it works, as well as how to do it better.  the dot (.) is the concatenation operator.  It is javascript's equivalent of the plus sign (+).  So looking at your original attempt of:

 

$mysql = $mysql + " Person ";

 

It really should have been

 

$mysql = $mysql . " Person ";

 

That is to say: concatenate the current value of $mysql and the string " Person " and assign the result to $mysql.

 

A better way to do it that doesn't require as much resources and time, is to use the shorthand version:

 

$mysql .=  " Person ";

 

This says: append the string " Person " to the end of $mysql.  The advantages to this are:

 

- Code is shorter shorter (less code to be parsed), so the script executes faster.

- Concept is less abstract.  If you visualize a string variable as lego blocks being stacked up.  It's easy to visualize just adding another block or two on top of the tower you're building (or whatever kids build these days).  Using the same lego blocks analogy with the previous example $mysql = $mysql . " Person ";, you would have to visualize your current tower of blocks, build another one that's a copy of it.  Add the new blocks to that.  And then move the new tower to the other tower's place and get rid of the other tower. So you see, it's easier to visualize what's going on in the code with the shorthand version. 

- Less resource consuming.  The computer is just adding more legos to the existing tower, instead of taking the time and effort to build another tower and all that noise. 

 

Here's the solution you came up with:

 

$mysql = $mysql .= " Where Name like '%$SearchA%' ";

 

This is even worse than doing

 

$mysql = $mysql . " Person ";

 

This code causes a copy of $mysql to be created twice.  The computer has to perform two separate operations in your code.  The inner operation is to concat the string to $mysql but first php has to make a copy of $mysql to do that inner operation.  Then another temp var is made, which is the result of that inner operation.  Then it is finally using in the outer operation, by being assigned to the original $mysql. 

 

Note:  differences in execution time, memory/resource consumption, etc.. are negligible.  Your computer probably has a dim idea of the difference, as it might struggle to perform the math at that kind of precision.  You and your users certainly will not notice the difference.

 

So saying your solution is "bad" wouldn't entirely be fair.  But it is the difference between good code and great code.

Just Figured it out, ARHHHH.

 

 

$mysql = " Select * from  ";           

$mysql = $mysql .= " Person ";

$mysql = $mysql .= " Where Name like '%$SearchA%' ";

 

Thanks Anyway

 

Though that technically works, I thought I'd tell you why it works, as well as how to do it better.  the dot (.) is the concatenation operator.  It is javascript's equivalent of the plus sign (+).  So looking at your original attempt of:

 

$mysql = $mysql + " Person ";

 

It really should have been

 

$mysql = $mysql . " Person ";

 

That is to say: concatenate the current value of $mysql and the string " Person " and assign the result to $mysql.

 

A better way to do it that doesn't require as much resources and time, is to use the shorthand version:

 

$mysql .=  " Person ";

 

This says: append the string " Person " to the end of $mysql.  The advantages to this are:

 

- Code is shorter shorter (less code to be parsed), so the script executes faster.

- Concept is less abstract.  If you visualize a string variable as lego blocks being stacked up.  It's easy to visualize just adding another block or two on top of the tower you're building (or whatever kids build these days).  Using the same lego blocks analogy with the previous example $mysql = $mysql . " Person ";, you would have to visualize your current tower of blocks, build another one that's a copy of it.  Add the new blocks to that.  And then move the new tower to the other tower's place and get rid of the other tower. So you see, it's easier to visualize what's going on in the code with the shorthand version. 

- Less resource consuming.  The computer is just adding more legos to the existing tower, instead of taking the time and effort to build another tower and all that noise. 

 

Here's the solution you came up with:

 

$mysql = $mysql .= " Where Name like '%$SearchA%' ";

 

This is even worse than doing

 

$mysql = $mysql . " Person ";

 

This code causes a copy of $mysql to be created twice.  The computer has to perform two separate operations in your code.  The inner operation is to concat the string to $mysql but first php has to make a copy of $mysql to do that inner operation.  Then another temp var is made, which is the result of that inner operation.  Then it is finally using in the outer operation, by being assigned to the original $mysql. 

 

Note:  differences in execution time, memory/resource consumption, etc.. are negligible.  Your computer probably has a dim idea of the difference, as it might struggle to perform the math at that kind of precision.  You and your users certainly will not notice the difference.

 

So saying your solution is "bad" wouldn't entirely be fair.  But it is the difference between good code and great code.

 

LOL cheers for the brill explanation i always try to code to shortest way i know possible, as I get more used to php i will be able to find quicker ways of coding.

 

Thanks

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.