How to Insert a Percent Symbol Beside a PHP String

Strings are chunks of text that you can save to variables and perform operations on when programming in languages such as PHP. In a PHP script, you can use a variable as a name that holds the text. Since you will typically reuse a string multiple times in a script, you do not always want to save symbols such as percent signs to the strings. The best way to output a percent symbol with a string is to instead add the symbol onto the string when it is output. This addition of extra text and other variables is called "concatenation."

Instructions

    • 1

      Open your PHP file in Notepad or a code editor. Find the line where you output the string:

      echo $my_string;

    • 2

      Add a dot between the variable name and the semicolon:

      echo $my_string . ;

      The dot will concatenate more text to a string, thereby changing the output of the "echo" statement.

    • 3

      Add your percent symbol between quotation marks after the dot. Use the HTML special character code for the percent symbol. Place a space in front of the character code to keep the percent symbol from bumping up against the text of the string:

      echo $my_string . ' &37;';

      When "my_string" is equal to "five," the output of the above "echo" statement is "Five %."

Related Searches:

Comments

Related Ads

Featured