Strings in PHP
So, strings in PHP are basically just text — anything inside quotes. You can wrap them in either "double quotes" or 'single quotes', but the trick is: double quotes interpret stuff like variables and special characters, while single quotes just treat everything as plain text.
For example:
$name = "MemberPress";
echo "Welcome to $name"; // Interprets $name
echo 'Welcome to $name'; // Literally prints "$name"
That’s one of the first “gotchas” when you’re debugging weird output.
Now, PHP gives you a bunch of built-in functions to play with strings. For instance:
strlen()→ counts characters.echo strlen("Pretty Links"); // 12str_word_count()→ counts words.echo str_word_count("ThirstyAffiliates rocks"); // 2strpos()→ finds a substring’s position.echo strpos("MemberPress Pro rocks", "Pro"); // 12
You can also modify strings easily:
strtoupper()/strtolower()to change case.str_replace()to replace part of a string.echo str_replace("Pro", "Basic", "MemberPress Pro"); // MemberPress Basicstrrev()flips the string around.trim()cleans whitespace from the edges — super handy when sanitizing user input.
When you need to split a string into pieces, use explode().
$plugins = "MemberPress,Pretty Links,ThirstyAffiliates";
$list = explode(",", $plugins);
print_r($list);
This turns it into an array.
If you want to combine strings instead, use the dot . operator:
$x = "MemberPress";
$y = "Rocks";
echo $x . " " . $y; // MemberPress Rocks
Or, if you’re lazy (like most of us 😅), double quotes handle that for you:
echo "$x $y";
For slicing strings, substr() is your friend. You choose where to start and how many characters to grab:
echo substr("Pretty Links", 7, 5); // Links
You can even use negative indexes to start from the end.
And finally, if you ever need to include something inside quotes that would normally break the syntax — like using quotes inside quotes — PHP’s got escape characters (a backslash \):
echo "We use \"Pretty Links\" for tracking.";
There are a bunch of these — \', \", \n, \t, and so on — basically for formatting or escaping special characters.