Jump to content

{} curly brace

- - - - -

  • Please log in to reply
5 replies to this topic

#1
dabid

dabid

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
When we are talking about variable, like in your example, it's mostly for the programmer preferences.
$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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.

$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.

#4
dabid

dabid

    Newbie

  • Members
  • Pip
  • 3 posts
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;
}
}

#5
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
My guess would be what $subject_id isn't valid, or is null, or empty

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
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




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users