Any tips on coming from PHP?

Im an “old school” PHP developer working in the traditional LAMP stack for years now. I’m ready for something new and have projects that need to scale. So naturally I’ve landed here.

I’ve purchased some elixir / Phoenix books and have the boot camp course on Udemy and about to really dive in.

But I have to admit, my brain is having a really hard time with the syntax! The functional programming part is fine grasping. But all the bizzare symbols everywhere just totally looses me. I don’t find it readable at all.

I know most of this is because it’s new, but even so, it looks so cryptic. Any tips on getting comfortable or understanding these basics. Sure I can copy and paste from a book or course, but I really want to understand what on earth I’m looking at.

I’ve heard amazing things about this community and excited to be here!

4 Likes

Welcome to Elixir and the forum :wave:!

Hmm, since the books seem like they aren’t working to well for you right now I’d recommend some more practical practice. I really like Exercism for that Elixir | Exercism because they’re nice little self-contained projects so they will help to get you familiar with reading and writing some elixir code.

If you haven’t already done it I would also recommend the official Getting Started guide: Introduction - The Elixir programming language

6 Likes

If you haven’t already done it I would also recommend the official Getting Started guide: Introduction - The Elixir programming language

I would also recommend the Getting Started guide. I also struggled with the syntax in the beginning, and I learned it by going through that guide. You should not just read it though - if you force yourself to type it all into IEx / .exs-files, your brain will eventually grok it.

When you’re done with the Getting Started guide I suggest also going through the Mix and OTP guide, just to get some experience with Mix.

4 Likes

A lot of developers learn most efficiently by doing, preferably with something useful at the end of doing, so if you have a small personal project to work on that can be a great platform for learning.

Feel free to ask questions on here. I think it’s fair to say that there’s a high proportion of enthusiasts who love the language, libraries and runtime after earning too many grey hairs from other platforms, and are more than happy to help beginners get going providing they are making some effort to self-learn along the way.

In terms of weird symbols etc, here are a couple of pointers to things that I found to be very different in the early days:

|> - pipe the output of one function into another as the first argument - pretty much like unix-style command line pipes.
:an_atom - this got me at first becomes I didn’t come from the school of Ruby - it’s basically a constant, but there are a few little tricks - you can google these. Here’s a starting point: syntax - Why is useful to have a atom type (like in elixir, erlang)? - Stack Overflow
~s(something or other) - a sigil - basically the contents within the brackets are processed in a special way depending on which sigil is used. Well documented in the language guide: Sigils - The Elixir programming language
<- and -> are just part of syntax in case & for - you’re just going to have to practise with those

There’s some really tight syntax for in-line defined functions can be super-confusing at first. For example (from Enumerables and Streams - The Elixir programming language):

1..100_000 |> Enum.map(&(&1 * 3))

This means, for each value between 1 and 100,000, multiple by 3.
The .. between 1 and 100_000 indicate the generation of a range. That range is passed in to Enum.map as the first argument by the |>, and an in-line function is passed in as the second argument.
&(&1 * 3) is the in-line function defined in place rather than being defined elsewhere and referenced. The & at the beginning indicates the start of the function definition and the &1 indicates the first (and only) variable passed into the function. There’s a little bit of discussion of this syntax here: Modules and functions - The Elixir programming language

While it seems overly terse at first, it turns out to be really useful as a way to define a simple transformation to apply to a list of data.

Finally, the whole pattern-matching thing is confusing if you haven’t come across it before. I tried to understand it by reading about it, but in the end I got my head around it by playing in the interactive shell. It’s like a combination of equality-checking and assignment depending on what elements of a match expression are able to be assigned to. Once you do grok it, pattern-matching is one of the language features that makes Elixir super-readable.

Again, feel free to ask about specific things…

6 Likes

Funny thing to hear from PHP developer.

5 Likes

:rofl:

But you have to admit 1..10 |> Enum.map(&(&1 + 2)) is a bit special when you first see it.

7 Likes

You don’t have to use all of them. Find a subset that you are comfortable with, and stick with it for a while. I’ve been writing elixir for a year and I still steer clear from with or list comprehension.

1 Like

I also come from PHP, and the only thing that clicked to me was this book:

or if you prefer the equivalent video course:

https://pragdave.me/

The hardest thing for me is to always understand what is compiled time, boot-time, and runtime, especially when it comes to configuration and to some macros and functions that are only called at compile time, and for us coming form languages like PHP keeps biting us over and over, but I am more aware now… with time you get better at spotting them :wink:

Another thing that is hard to grasp is the Dyalizer type specs, and this may be the symbols that you are complaining about, because they also puzzled me in the begin, but nowadays I learned to “ignore” them, and I just use the Domo library that hides their complexity from being everywhere in my code.

2 Likes

Thanks so much! I actually just discovered Exercism and joined the Elixir and Rust tracks, and will likely be a PHP mentor there as well! So this is a great tip.

To be fair, I wouldn’t say the books aren’t workign… i’m positive I can get things working by following the books. It was more wishing there was some kind of cheat sheet for the syntax. But perhaps i’m putting the cart before the horse here. I got a Phoenix / Elixir book. Maybe I need to dive into elixir itself first

I was going through some basic Phoenix examples last night and saw this:

def register_user(attrs \\ %{}) do
 	  %User{}
 	  |> User.registration_changeset(attrs)
 	  |> Repo.insert()
 end

Most of that isn’t words at all. What on earth is \ and %{}? Thats the stuff i’m talking about. A similar function in my php app is:

public function add( $p_asPayload ) {
     $p_asPayload['client_id'] = DB_CLIENT_ID;
     $this->db->insert( 'lessons', $p_asPayload );
     return $this->db->insert_id();
}

Short of the $ symbol… it’s all humanly readable. Trust me, i’m not saying PHP is better… but I do feel like it’s less cryptic, even if that means it’s less functional.

Thanks! I actually had a demo of this book, but then picked up the Pragmatic Programmers Programming Phoenix > 1.4 book. And i’m starting to see that, this is the issue. I might have to spend some more time in elixir itself before diving into phoenix, plugs and web routing.

Thanks for the reminder about this book!

1 Like

It’s because You are not familiar (yet) with the syntax.

It should be attrs \\ %{} (with 2 backslahes)

It’s just a way to define default value… like attrs = default would be in js.

The cryptic %{} defines a map, and %User{} is a struct. You might see there is a rich set of datatypes.

  • [] list, THIS IS NOT AN ARRAY!
  • {} tuple
  • %{} map
  • %User{} struct

These datatypes are common in FP, and You need to learn properly how to use them.

Instead of chaining, like OOP would do, You find pipes (|>) in Elixir, where You provides data to a functional pipeline. Instead of this->db->insert_id() You will find id |> insert_id(), with the Repo pattern.

Please use ``` to wrap your code, it will make it more readable, as in markdown.

3 Likes

Programming Phoenix 1.4 is a good book, but definitely not the one you want to start from. It’s like landing in PHP and start learning Laravel or Symphony first :slight_smile:

The book I recommend you also as an introduction to Phoenix, at the end, but before it teaches you to write applications with Elixir only.

2 Likes

I would argue otherwise. I have never written php and my previous experience was with python/java/js. I’m still not as comfortable with lot of elixir/fp stuff, but to me, the elixir syntax is much easier to understand than the php function you have written. It is because I’m now familiar with elixir syntax, similar to how you are familiar with php.

1 Like

I really recommend the getting started as mentioned above, it goes over pretty much all the syntax of the language, for example here are the links for the default arguments and maps that you are having trouble with.

A big, big difference between PHP and Elixir is that in Elixir you can actually follow official docs and guides. If you need to know what functions or modules are available to you, go to HexDocs and look for what you need. You can be sure what you read there works, with examples and all. Unlike PHP docs where you have to scroll down to the comments section to know if what you’re seeing actually works and you’re not about to shoot yourself on the foot.

Also, once you know the basic elixir syntax, the rest of the constructs are quite intuitive. It’s quite well put together imo.

2 Likes

Thank you… seems obvious now, but perhaps I was in a bit of a rush :slight_smile:

1 Like

Thank you… def helps. I’m realizing my big mistake was jumping right into a Phoenix book without first taking the time to get to know Elixir itself

2 Likes

Here is your cheatsheet :wink:

8 Likes

Welcome to the community!

My small piece of advice is this: As you go along, get to know and get comfortable with iex (and later, it’s Erlang parent, erl).

Coming from PHP, you’re probably not use to working in a REPL like iex. When developing in PHP, you’re use to using print_r, var_dunp etc., to dump output onto a webpage and/or finding errors in the error log.

When using iex -S mix or iex -S mix phx.server to run an app in development, everything is available to you within iex. Once the app is running, you can inspect and interact with running code and iex is basically just another set of concurrent processes that’s running in the app. Here’s something fun to try. You can even kill it and see it restart: iex(1)> Process.exit(self(), :kill) without having any affect on the running app.

I could go on :slight_smile:

Once you get past some of the issues you now face, and get use to working this way, you’ll start to wonder how you could ever do app development any other way!

In my daily work with Elixir, iex is an indispensable tool, and with time, I’m sure you’ll start to feel that way too.

4 Likes

If you are in a rush
https://learnxinyminutes.com/docs/elixir/