PHP URL

Beginner
7 min read
Updated: Nov 2026
3 examples
Encode, parse & build

Definition and Usage

The PHP URL functions help you build, read, and safely encode web addresses. They turn text into a form that can travel in a URL and back again.

Use urlencode() for query values, rawurlencode() for paths, http_build_query() to make a whole query string, and parse_url() to take a URL apart. base64_encode() and base64_decode() turn data into text.

This page explains the ideas. The URL Functions index lists all 10 function guides.

Encode each part.

Encode every value on its own with urlencode() or rawurlencode(). If you encode a whole URL, the : and / characters break.

PHP Syntax

The most common URL tasks in one place:

PHP
<?php
// Encode a value for a query string
$q = urlencode("php & mysql");            // php+%26+mysql

// Build a whole query string from an array
$query = http_build_query(["q" => "php", "page" => 2]);   // q=php&page=2

// Split a URL into parts
$parts = parse_url("https://example.com/a?x=1");

// Turn data into base64 text and back
$b64  = base64_encode("Hello");           // SGVsbG8=
$text = base64_decode($b64);              // Hello

Core Ideas

What to remember when you work with URLs in PHP:

IdeaExampleRemember
Query valueurlencode($text)A space becomes +. Use it for form-style data.
Path piecerawurlencode($text)A space becomes %20. Use it for URL paths.
Whole queryhttp_build_query($array)Builds a=1&b=2 and encodes every value.
Split a URLparse_url($url)Returns scheme, host, path, query and more.
Base64base64_encode($data)Turns data into plain text. It is not encryption.
Page infoget_headers($url)Needs network access. Reads the response headers.

PHP Examples

Three short scripts that show how the URL functions are used.

Example 1 — Encode a Query Value

Make text safe to place in a URL.

PHP
<?php
$term = "php & mysql";

echo "https://example.com/search?q=" . urlencode($term), PHP_EOL;

How It Works

  1. urlencode() changes spaces to + and & to %26.
  2. Only the value is encoded. The base URL stays as it is.
  3. The pieces are joined with `. ` into one link.
  4. Use rawurlencode() instead when the text goes in a path.

Example 2 — Build a Query String

Turn an array into the part after the question mark.

PHP
<?php
$params = ["q" => "php", "page" => 2];

echo "https://example.com/list?" . http_build_query($params), PHP_EOL;

How It Works

  1. Each array key becomes a parameter name.
  2. Each value is encoded for you.
  3. The pairs are joined with &.
  4. You add the ? yourself.

Example 3 — Split a URL into Parts

Read the host and path from a link.

PHP
<?php
$url = "https://example.com/blog/post?id=7";

$parts = parse_url($url);

echo $parts["host"], PHP_EOL;
echo $parts["path"], PHP_EOL;

How It Works

  1. parse_url() returns an array of parts.
  2. host holds the domain name.
  3. path holds the part after the domain.
  4. Missing parts are not in the array, so check with isset().

Next: PHP URL Functions Index

After you know the basics, open the function index for all 10 guides.

Start withWhy
urlencode()Encode a query value
rawurlencode()Encode a URL path piece
http_build_query()Build a query string from an array
parse_url()Split a URL into parts
base64_encode()Turn data into base64 text
get_headers()Read the headers of a web page

Full list: PHP URL Functions index (/php/url/function).

Frequently Asked Questions

They are the functions that build, read and encode web addresses. The main ones are urlencode(), rawurlencode(), http_build_query(), parse_url(), base64_encode() and base64_decode().
urlencode() writes a space as + and is meant for form-style query values. rawurlencode() writes a space as %20 and is meant for URL paths.
Use http_build_query($array). It encodes each value and joins the pairs with &.
Use parse_url($url) for an array of parts, or pass a PHP_URL_* constant to get one part.
No. Base64 is only an encoding. Anyone can decode it, so do not use it to hide secrets.
Practice the examples here, then open the URL Functions index for all 10 function guides.

Browse every URL function

Open the functions index for categorized guides with examples and FAQs.

URL Functions Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful