Example 1 — Encode Text for a Query
Replace spaces and special characters.
<?php
$text = "Hello World & Co";
echo urlencode($text), PHP_EOL; How It Works
- Start with plain text. It has spaces and an
&. - Call urlencode(). Each space becomes
+. - The
&becomes%26. So it will not split a query pair. - Print the result. It is safe to place in a URL.

