hello, i am new here, and very new using a programming language, i am currently studying php, and i just want to know if what is the use of curly brace, when using it on a variable, what is the meaning if i will put the variable inside the curly brace? something like this e.g "{$variable}". thnx.
5 replies to this topic
#1
Posted 23 September 2011 - 01:33 AM
|
|
|
#2
Posted 23 September 2011 - 04:26 AM
When we are talking about variable, like in your example, it's mostly for the programmer preferences.
So in your exemple, it dosen't do anything, but it can create variable from variable... BUT if you want to do this, you should really take a look at arrays, much cleaner and easier to understand
$var1 = 'Hello World!';is the same as
${'var1'} = 'Hello World!';And it's the same as${'var' . '1'} = 'Hello World!';
So in your exemple, it dosen't do anything, but it can create variable from variable... BUT if you want to do this, you should really take a look at arrays, much cleaner and easier to understand
#3
Posted 23 September 2011 - 07:04 AM
I believe you are talking about it, in the context of a string.
Lets assume we have an integer called $apples, holding the amount of apples we've consumed.
You could output a useful string, to inform the doctor when the last apple was consumed (so he knows you did not eat them all on the first day):
A silly example, however you can see that the "th" must be placed after 15, however it unfortunately is a part of the variable name now and $applesth does not exist.
You can separate the two:
You can as well substitute for an array entry with that syntax:
It all depends on your choice of syntax, some prefer "..." . $apples . "th"
Lets assume we have an integer called $apples, holding the amount of apples we've consumed.
$apples = 15; //we want to be healthy today
You could output a useful string, to inform the doctor when the last apple was consumed (so he knows you did not eat them all on the first day):
echo "The $applesth and last apple, I had this morning.";
A silly example, however you can see that the "th" must be placed after 15, however it unfortunately is a part of the variable name now and $applesth does not exist.
You can separate the two:
" .. {$apples}th .."
You can as well substitute for an array entry with that syntax:
$days = array("monday", "tuesday", "wednesday");
echo "The second day: {$days[1]}"
It all depends on your choice of syntax, some prefer "..." . $apples . "th"
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#4
Posted 08 November 2011 - 06:24 AM
thank you very much sir, sory for the late reply, i really appreciate it, sir i have another question if its ok," You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 1", this is the error i got, i am in the middle of video tutorial by lynda.com, this is a query. but in the tutorial they put limit 1, its working on the video tutorial that i watch, but for me, it doesnt work, here is the code sir
function get_subject_by_id($subject_id) {
global $connection;
echo $query = "SELECT *
from subjects
where id = $subject_id
limit 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
function get_subject_by_id($subject_id) {
global $connection;
echo $query = "SELECT *
from subjects
where id = $subject_id
limit 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
#5
Posted 08 November 2011 - 06:30 AM
My guess would be what $subject_id isn't valid, or is null, or empty
#6
Posted 08 November 2011 - 08:41 AM
normally, when handling id:s, you don't need a limit, as they are hopefully unique in the table. Otherwise, as Vaielab said.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









