https://github.com/ascorbic/use-php
Use PHP in React
https://github.com/ascorbic/use-php
Last synced: 6 months ago
JSON representation
Use PHP in React
- Host: GitHub
- URL: https://github.com/ascorbic/use-php
- Owner: ascorbic
- Created: 2022-08-10T13:45:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-07T07:26:38.000Z (about 2 years ago)
- Last Synced: 2025-06-08T01:42:53.204Z (8 months ago)
- Language: TypeScript
- Homepage: https://use-php.netlify.app
- Size: 4.1 MB
- Stars: 206
- Watchers: 6
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# 🏎️ usePHP 💨
A React hook for running PHP? Why not?
It runs in a Netlify Edge Function and uses [php-wasm](https://github.com/seanmorris/php-wasm) to execute the PHP. It runs asynchronously, so you need a renderer that supports async components.
**[View the demo](https://php-edge.netlify.app/)**
[](https://app.netlify.com/start/deploy?repository=https://github.com/ascorbic/use-php)
### Usage
```tsx filename=netlify/edge-functions/php.tsx
/** @jsx h */
import { h, renderToString } from "https://deno.land/x/jsx/mod.ts";
import { usePHP } from "https://deno.land/x/use_php/mod.ts";
export default async function handler(request: Request) {
const php = await usePHP(request, h);
return new Response(
await renderToString(
Use PHP
{await php`
`}
),
{
headers: {
"content-type": "text/html; charset=UTF-8",
},
}
);
}
export const config = {
path: "/*",
excluded_paths: "/public/*",
};
```
## API
### `usePHP(request: Request, renderer?: JSXRenderer)`
Pass in the request object and an optional JSX renderer and it will return tagged template that you can use to execute PHP. If you pass a JSX render function to the hook (e.g. `h` or `React.createElement`), then the template will return a JSX element. If you omit the renderer it will return an HTML string instead.
Try it yourself:
[](https://app.netlify.com/start/deploy?repository=https://github.com/ascorbic/use-php)
## FAQ
- **Why?**
- Why not? That Lambo won't buy itself.
- **How can I deploy this to production?**
- 🤦🏻♂️
### LICENCE
MIT Licence. © 2023 [Matt Kane](https://github.com/ascorbic)