Skip to content

Commit 9a991e7

Browse files
committed
Add PHP to Zephir string conversion requirements
Added critical documentation about converting PHP strings to Zephir: **Key Addition:** When converting PHP code to Zephir, all single-quoted strings must be converted to double-quoted strings and properly escaped following the same escape rules as double-quoted strings in PHP. **Why This Matters:** - In PHP: Both 'text' and "text" create strings - In Zephir: 'x' creates a char, "text" creates a string - Single quotes in Zephir are exclusively for single characters (char type) **Changes Made:** 1. **Dynamic Types Section:** - Added note that strings must use double quotes (unlike PHP) - Added "PHP to Zephir" conversion reminder 2. **String vs Char Section:** - Added dedicated "Important for PHP to Zephir Conversion" subsection - Provided side-by-side PHP vs Zephir code examples - Showed escape sequence handling differences 3. **Common Pitfalls:** - Added conversion reminder to string vs char confusion item This prevents a common mistake when porting PHP code to Zephir where developers might try to use single quotes for strings, which would create char types instead.
1 parent 1e718d7 commit 9a991e7

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

turbo-ext/CLAUDE.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ Dynamic variables support **eight types**:
268268
- Only long and string values function as keys
269269

270270
**Strings:**
271-
- Require double quotes
271+
- **Must use double quotes** (unlike PHP where single quotes also work)
272272
- Support escape sequences (`\t`, `\n`, `\r`, `\\`, `\"`)
273273
- Variable interpolation isn't supported; concatenation is required instead
274+
- **PHP to Zephir**: Convert all single-quoted PHP strings (`'text'`) to double-quoted (`"text"`)
274275

275276
### Static Types
276277

@@ -320,6 +321,25 @@ Understanding the difference between `string` and `char` types is critical in Ze
320321
- **String**: Uses double quotes `"hello"`
321322
- **Char**: Uses single quotes `'h'`
322323

324+
**Important for PHP to Zephir Conversion:**
325+
When converting PHP code to Zephir, **all single-quoted strings must be converted to double-quoted strings** and properly escaped following the same rules as double-quoted strings in PHP:
326+
327+
```php
328+
// PHP - both single and double quotes work for strings
329+
$str1 = 'hello world';
330+
$str2 = "hello world";
331+
$str3 = 'it\'s a test';
332+
```
333+
334+
```zephir
335+
// Zephir - MUST use double quotes for strings
336+
string str1 = "hello world";
337+
string str2 = "hello world";
338+
string str3 = "it's a test"; // No need to escape single quote in double-quoted string
339+
```
340+
341+
Remember: In Zephir, single quotes create a `char` type (single character), not a `string`!
342+
323343
**Array Access Returns Char:**
324344
When you access a string by index, Zephir returns a `char`, not a `string`:
325345

@@ -1835,6 +1855,7 @@ vendor/bin/zephir generate && vendor/bin/zephir compile
18351855
- Use single quotes for char: `'a'`, double quotes for string: `"a"`
18361856
- When iterating strings, loop variable is `char` type
18371857
- Comparing `str[0] == "a"` is wrong; use `str[0] == 'a'` instead
1858+
- **PHP to Zephir**: Convert all single-quoted PHP strings to double-quoted Zephir strings
18381859
4. **Missing Semicolons**: While optional, they can help avoid ambiguity
18391860
5. **File/Class Mismatch**: File structure must match namespace and class names
18401861
6. **Immutable Variables**: Use `let` to assign values, not direct assignment

0 commit comments

Comments
 (0)