Jump to content

when do we need curly brackets?


wishbone
 Share

Recommended Posts

What you think how much time I have spent with try and error and so any coder living except maybe Woz. It's part of the business and if one is not willing to try he get nowhere. When I make designs it's practically the same.

  • Like 4
Link to comment
Share on other sites

What you think how much time I have spent with try and error and so any coder living except maybe Woz. It's part of the business and if one is not willing to try he get nowhere. When I make designs it's practically the same.

yes of course - but we get stuck! We couldn't continue without your help which makes us very vulnerable...

Link to comment
Share on other sites

put that code in a php file, open it in the browser and see what happens :)

I started with xampp long time ago but gave up on it and moved over to wamp. Really recommended for website testing, experiments, checking different php version compatibility, debugging, etc.

Link to comment
Share on other sites

Yes that's exactly how it is. I am going through the same phase. Not born with a programmers/coders mind but mad about processwire. It's a hard fight. But lots can be done with only the api of processwire. Stick to that first and in the mean time learn php to it. Works for most designers. And yes thanks to this forum with a lot of helping pro coders things are really good here.

Heh,  I also often feel like I'm banging my head against a brick wall when I code. But I guess one day you do break through it.  

Side note: I actually haven't had any success with curly brackets either, until reading this post. I don't know why, just whenever I used them they never worked until trying it again now. So guess you're not the only one! 

  • Like 2
Link to comment
Share on other sites

The basics of programming I learned from Filemaker Pro. (That sounds weird)

13 years ago I started as freelance Scaffolder in in the stage building business. On that time, I really messed up my book keepings and I needed a simple but effective Invoice system. In that time I build occasionally websites with pure HTML, wished to use some PHP, but couldn't wrap my mind around it.

Then I found Filemaker. I start building the system and came across a totally new terrain for me (programming). I came across database relations and I really needed to script it. The if statements and database relations pilled up, but at the end I had a simple and effective Invoice system.

This is where I learned some basics of scripting, after that my mind was set, and I could start with PHP. 

  • Like 7
Link to comment
Share on other sites

hi wishbone,

so html inside php is a string?

the german word for string is "zeichenkette", that is, every number of letters and numbers that are echoed as pure text to your code. for example, numbers that are echoed as string can't be used for doing any math anymore because they are part of a string, a "zeichenkette".

does that help?

Link to comment
Share on other sites

  • 7 months later...

What confused me in connection with curly brackets is the different behaviour when using single or double quotes.

Starting with double quotes, e.g.

$carouselId = 10;
$carouselMarkup = "<div id='carousel-{$carouselId}'>"

Result markup

<div id="carousel-10">

BUT, starting with single quotes

$carouselMarkup = '<div id="carousel-{$carouselId}">'

Result markup

<div id="carousel-{$carouselId}">

I just spent an hour until I realised that starting with double quotes is important when using variables inside a string (no matter if curly brackets or not).

Link to comment
Share on other sites

Or you can do:

$carouselMarkup = '<div id="carousel-'.$carouselId.'">';

A period in php is a concatenator, same as + is in javascript. The advantage to this approach is that it lets you apply a function to the variable, like:

$carouselMarkup = '<div id="carousel-'.strtolower($carouselId).'">';

And actually you don't even need the curly braces inside double quotes if you are using a simply variable. It's only if you trying to do something like:

$page->id that you need the curly braces surrounding everything. So this would work just as well:

$carouselMarkup = "<div id='carousel-$carouselId'>";

So, it's good to have all these in your repertoire.

  • Like 2
Link to comment
Share on other sites

Or you can do:

$carouselMarkup = '<div id="carousel-'.$carouselId.'">';

That's what I actually did before. But then I wanted to get rid off the extra periods and single quotes to make the code shorter and more readable.

It's only if you trying to do something like:

$page->id that you need the curly braces surrounding everything. So this would work just as well:

I have this code and it is working well:

$carouselMarkup .= "<div class='item $class'>

          <img src='$image->url' alt='$image->description' width='$image->width' height ='$image->height'>

...

note that $image->url is not in curly braces. This seems to prove your assumption wrong (no offense intended).

So, it's good to have all these in your repertoire.

Definitely :-)

Link to comment
Share on other sites

No offense taken. I have never been much of a curly brace guy - I was always into single quotes and concatenating. I know this goes against the general consensus of what's easier to read, but for some reason it seems more logical to me.

You should also have a read of this post: https://processwire.com/talk/topic/4439-when-do-we-need-curly-brackets/

  • Like 2
Link to comment
Share on other sites

I use a mixture of both - oops! I have come to love concatenating though. Using a text editor with syntax highlighting like ST, I can clearly see what is a string and what is a variable, unlike variables within curly braces within a string; these get the same colour as the string. 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...