You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nil, true and false are literal constants. In Phel, `nil` is the same as `null` in PHP. Phel's `true` and `false` are the same as PHP's `true` and `false`.
8
+
Nil, true and false are literal constants.
9
9
10
10
```phel
11
11
nil
12
12
true
13
13
false
14
14
```
15
15
16
+
In Phel, only `false` and `nil` are falsy. Everything else is truthy—including `0`, `""`, and `[]`.
In PHP, `nil` is the same as `null`, and `true`/`false` are the same. However, truthiness works differently:
29
+
30
+
**PHP**: `0`, `""`, `[]`, `null`, and `false` are all falsy
31
+
**Phel**: Only `false` and `nil` are falsy
32
+
33
+
This means `if (0)` in PHP is false, but `(if 0 ...)` in Phel is true!
34
+
{% end %}
35
+
36
+
{% clojure_note() %}
37
+
Truthiness is the same as Clojure—only `false` and `nil` are falsy.
38
+
{% end %}
39
+
16
40
## Symbol
17
41
18
42
Symbols are used to name functions and variables in Phel.
@@ -26,7 +50,7 @@ my-module/my-function
26
50
27
51
## Keywords
28
52
29
-
A keyword is like a symbol that begins with a colon character. However, it is used as a constant rather than a name for something.
53
+
A keyword is like a symbol that begins with a colon character. However, it is used as a constant rather than a name for something. Keywords are interned and fast for equality checks.
30
54
31
55
```phel
32
56
:keyword
@@ -36,6 +60,39 @@ A keyword is like a symbol that begins with a colon character. However, it is us
36
60
::
37
61
```
38
62
63
+
Keywords are commonly used as map keys and function options:
Keywords are like string constants, but more efficient for map keys. Use keywords instead of strings for map keys:
80
+
81
+
```phel
82
+
# Less idiomatic:
83
+
{"name" "Alice" "age" 30}
84
+
85
+
# Idiomatic:
86
+
{:name "Alice" :age 30}
87
+
```
88
+
89
+
Keywords are interned (only one instance exists in memory), making equality checks very fast.
90
+
{% end %}
91
+
92
+
{% clojure_note() %}
93
+
Keywords work exactly like in Clojure—they're interned, fast for equality checks, and self-evaluate.
94
+
{% end %}
95
+
39
96
## Numbers
40
97
41
98
Phel supports integers and floating-point numbers. Both use the underlying PHP implementation. Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2) notations. Binary, octal and hexadecimal formats may contain underscores (`_`) between digits for better readability.
@@ -69,9 +126,7 @@ Phel supports integers and floating-point numbers. Both use the underlying PHP i
69
126
70
127
## Strings
71
128
72
-
Strings are surrounded by double quotes. They almost work the same as PHP double-quoted strings. One difference is that the dollar sign (`$`) must not be escaped. Internally, Phel strings are represented by PHP strings. Therefore, every PHP string function can be used to operate on the string.
73
-
74
-
Strings can be written over multiple lines. The line break character is then ignored by the reader.
129
+
Strings are surrounded by double quotes. The dollar sign (`$`) does not need to be escaped.
75
130
76
131
```phel
77
132
"hello world"
@@ -89,9 +144,28 @@ string."
89
144
90
145
"Hexadecimal notation is supported: \x41"
91
146
92
-
"Unicodes can be encoded as in PHP: \u{1000}"
147
+
"Unicodes can be encoded: \u{1000}"
148
+
```
149
+
150
+
String concatenation and conversion using `str`:
151
+
152
+
```phel
153
+
(str "Hello" " " "World") # => "Hello World"
154
+
(str "The answer is " 42) # => "The answer is 42"
155
+
```
156
+
157
+
{% php_note() %}
158
+
Phel strings are PHP strings internally, so you can use all PHP string functions:
159
+
160
+
```phel
161
+
(php/strlen "hello") # => 5
162
+
(php/strtoupper "hello") # => "HELLO"
163
+
(php/str_replace "o" "0" "hello") # => "hell0"
93
164
```
94
165
166
+
Strings work almost the same as PHP double-quoted strings, with one difference: the dollar sign (`$`) doesn't need escaping.
167
+
{% end %}
168
+
95
169
## Lists
96
170
97
171
A list is a sequence of whitespace-separated values surrounded by parentheses.
@@ -118,17 +192,42 @@ A vector in Phel is an indexed data structure. In contrast to PHP arrays, Phel v
118
192
119
193
## Maps
120
194
121
-
A map is a sequence of whitespace-separated key/value pairs surrounded by curly braces, wherein the key and value of each key/value pair are separated by whitespace. There must be an even number of items between curly braces or the parser will signal a parse error. The sequence is defined as key1, value1, key2, value2, etc.
195
+
A map is a sequence of whitespace-separated key/value pairs surrounded by curly braces. The sequence is defined as key1, value1, key2, value2, etc. There must be an even number of items.
0 commit comments