Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Does someone have a good definition for highlighting PHP code with the listings package?

share|improve this question
According to the manual a PHP definition is provided. –  qubyte May 1 '12 at 11:22
Sure, but there are no colours in this definition? –  Sam May 1 '12 at 11:25
See the manual for details on how to change the default monochrome appearance. Note that this is not actually to do with PHP or any other language. You assign a style (with a colour if you choose) to something such as a string, and listings determines what part of the source code is a string and applies your style. Therefore you styling can apply to any language. See sections 1.3 and 2.8 of the manual for an example. –  qubyte May 1 '12 at 11:33
I'm aware of. My question is, does someone have a such definition, which meet the code specialities of php? –  Sam May 1 '12 at 11:42
Perhaps not too helpful, but for the former take a look at this: tex.stackexchange.com/questions/42889/… Ignore the literate part of that style as it's not really necessary. Maybe it'll give you a good starting point. –  qubyte May 1 '12 at 11:57

1 Answer 1

up vote 5 down vote accepted

listings provides lots of options to color/customize the format of the style to your likings. Of course you should choose php as a language.

This may be a starting point:



  language        = php,
  basicstyle      = \small\ttfamily,
  keywordstyle    = \color{dkblue},
  stringstyle     = \color{red},
  identifierstyle = \color{dkgreen},
  commentstyle    = \color{gray},
  emph            =[1]{php},
  emphstyle       =[1]\color{black},
  emph            =[2]{if,and,or,else},
  emphstyle       =[2]\color{dkyellow}}


/* this is a stupid example */
$username = $_POST["username"];
$passwort = $_POST["passwort"];

$pass = md5($passwort);

// another comment
if($username=="Andavos" and
   echo "Herzlich Willkommen";
   echo "Login Fehlgeschlagen";


enter image description here

share|improve this answer
FYI: meta.tex.stackexchange.com/questions/2414/… I think tat this question probably qualifies. As it stands, it seems ambiguous and by answering it lowers the odds that the question will be improved to bring it up to standard. –  qubyte May 7 '12 at 9:59
I think it should stay now that it's here. If the question doesn't improve then the question should be closed. –  qubyte May 7 '12 at 10:11
I'm still hoping that it'll be updated. –  qubyte May 7 '12 at 10:15
Why this works when it is not included? When I include it: i.imgur.com/MdMrn38.png –  ilhan Jun 21 '13 at 21:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.