The cause
The cause of this error, though hard for a novice in PHP to find, was very simple. I had blank lines before the first line of PHP in my PHP file. It can also occur from having erroneous spaces there.
Don’t have any leading new lines or spaces as the first characters in your primary php file.
I can only assume that this occurred differently on my local machine compared to the hosting provider because of PHP versioning difference. So while I didn’t dig into this any further, here are the PHP version details of each—Incase it helps anyone in the future. Though it could have been caused by something else.
My staging site on my hosting space.
PHP version: 8.1.16
The error occurred in this setup
My local development setup (Run by Local)
Run by Local
PHP version: 8.1.9
The error didn’t occur in this setup.
Both setups were running WordPress 6.2.2