# [Discussion][RFC Idea] Cast Variables by Reference

- Thread: https://externals.io/message/127938
- Original archive: https://news-web.php.net/php.internals/127938
- Started by Joseph Leedy on 2025-07-07T16:22:20+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #127938 — Joseph Leedy — 2025-07-07T16:22:20+00:00

URL: https://externals.io/message/127938#127938

Hello, I'm Joseph, or Seph for short. I'm a long time listener, but a 
first-time caller. I've had an idea rattling around in my noggin for a while 
that I'd like your feedback on:

As a developer, I would like to have variables of one type cast to another 
type automatically so that I do not need to assign the variable to itself.

Instead of the following:

```php
$price = '5.0123';
...
$price = (float) $price;

var_dump($price); // Result: `float: 5.0123`
```

What if we could do this instead?

```php
$price = '5.0123';
...
(float) $price;

var_dump($price); // Result: `float: 5.0123`
```

If everyone thinks this a good idea, I would be willing to put together an 
RFC and work on the implementation myself, with guidance from an experienced 
core developer. I have no experience with C/C++, other than a brief foray 
more than twenty years ago, but I am willing to learn.

Thank you for your consideration! Also, a huge thank you to everyone who 
has made PHP what it is today! 

(**Note**: No AI was used in the writing of this message.)

---

## #127939 — Rob Landers — 2025-07-07T16:41:28+00:00

URL: https://externals.io/message/127938#127939 · In reply to: #127938

> Hello, I'm Joseph, or Seph for short. I'm a long time listener, but a 
> first-time caller. I've had an idea rattling around in my noggin for a while 
> that I'd like your feedback on:
> 
> As a developer, I would like to have variables of one type cast to another 
> type automatically so that I do not need to assign the variable to itself.
> 
> Instead of the following:
> 
> ```php
> $price = '5.0123';
> ...
> $price = (float) $price;
> 
> var_dump($price); // Result: `float: 5.0123`
> ```
> 
> What if we could do this instead?
> 
> ```php
> $price = '5.0123';
> ...
> (float) $price;
> 
> var_dump($price); // Result: `float: 5.0123`
> ```
> 
> If everyone thinks this a good idea, I would be willing to put together an 
> RFC and work on the implementation myself, with guidance from an experienced 
> core developer. I have no experience with C/C++, other than a brief foray 
> more than twenty years ago, but I am willing to learn.
> 
> Thank you for your consideration! Also, a huge thank you to everyone who 
> has made PHP what it is today! 
> 
> (**Note**: No AI was used in the writing of this message.)
> 

Hey Seph,

This is basically how non-strict mode works; there's no need for a cast. (I very rarely use strict mode, so I was a little confused on why you'd ever want to do this)

— Rob

---

## #127940 — Rob Landers — 2025-07-07T16:45:57+00:00

URL: https://externals.io/message/127938#127940 · In reply to: #127939

> 
> 
> 
> > […]
> 
> Hey Seph,
> 
> This is basically how non-strict mode works; there's no need for a cast. (I very rarely use strict mode, so I was a little confused on why you'd ever want to do this)
> 
> — Rob

Aaaand, I just discovered ctrl-enter sends the email.

But to continue on with what I was saying, casting is relatively dangerous (which is why I stay away from strict mode).

https://3v4l.org/6VvWc#vnull

vs.

https://3v4l.org/1cQ9l#vnull

— Rob

---

## #127941 — drealecs@gmail.com — 2025-07-07T16:47:14+00:00

URL: https://externals.io/message/127938#127941 · In reply to: #127938

On Mon, Jul 7, 2025 at 7:25 PM Joseph Leedy <joseph+php@josephleedy.dev>
wrote:

>
> As a developer, I would like to have variables of one type cast to another
> type automatically so that I do not need to assign the variable to itself.
>
>
> What if we could do this instead?
>
> ```php
> $price = '5.0123';
> ...
> (float) $price;
>
> var_dump($price); // Result: `float: 5.0123`
> ```
>
>
This is valid code now, and does not perform the cast.
You need to use a slightly different syntax, maybe `(float)&$price;` that
is an error now.

-- 
Alex

---

## #127942 — Joseph Leedy — 2025-07-07T16:50:17+00:00

URL: https://externals.io/message/127938#127942 · In reply to: #127941

> 
> You need to use a slightly different syntax, maybe `(float)&$price;` that is an error now.

Apologies; I missed the ampersand in my example solution.

---

## #127944 — Joseph Leedy — 2025-07-07T16:57:43+00:00

URL: https://externals.io/message/127938#127944 · In reply to: #127942

> This is basically how non-strict mode works; there's no need for a cast. (I very rarely
> use strict mode, so I was a little confused on why you'd ever want to do this)

I always code in strict mode, so I find myself having to cast variables quite often.

---

## #127947 — Claude Pache — 2025-07-07T17:54:49+00:00

URL: https://externals.io/message/127938#127947 · In reply to: #127938

> Le 7 juil. 2025 à 18:22, Joseph Leedy <joseph+php@josephleedy.dev> a écrit :
> 
> Hello, I'm Joseph, or Seph for short. I'm a long time listener, but a 
> first-time caller. I've had an idea rattling around in my noggin for a while 
> that I'd like your feedback on:
> 
> As a developer, I would like to have variables of one type cast to another 
> type automatically so that I do not need to assign the variable to itself.
> 
> Instead of the following:
> 
> ```php
> $price = '5.0123';
> ...
> $price = (float) $price;
> 
> var_dump($price); // Result: `float: 5.0123`
> ```
> 
> What if we could do this instead?
> 
> ```php
> $price = '5.0123';
> ...
> (float) $price;
> 
> var_dump($price); // Result: `float: 5.0123`
> ```
> 
> If everyone thinks this a good idea, I would be willing to put together an 
> RFC and work on the implementation myself, with guidance from an experienced 
> core developer. I have no experience with C/C++, other than a brief foray 
> more than twenty years ago, but I am willing to learn.
> 
> Thank you for your consideration! Also, a huge thank you to everyone who 
> has made PHP what it is today! 
> 
> (**Note**: No AI was used in the writing of this message.)

Hi,

There is already a built-in function for that very purpose, namely `settype()`, see:

https://www.php.net/settype


—Claude

---

## #127948 — Joseph Leedy — 2025-07-07T18:00:32+00:00

URL: https://externals.io/message/127938#127948 · In reply to: #127947

On Mon, Jul 7, 2025, at 11:54, Claude Pache wrote: 
> Hi,
> 
> There is already a built-in function for that very purpose, namely `settype()`, see:
> 
> https://www.php.net/settype
> 
> 
> —Claude

Thanks, Claude. Over twenty years of PHP development and I didn't know that function exists (or I forgot about it). TIL! Still, this function seems clunkier than a straight cast would be.
