<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>speaker &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/speaker/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>5 PHP String Functions You Need to Know</title>
		<link>/2010/09/17/5-php-string-functions-you-need-to-know/</link>
		<comments>/2010/09/17/5-php-string-functions-you-need-to-know/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 11:19:43 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Case Asset Management AB]]></category>
		<category><![CDATA[Foo bar]]></category>
		<category><![CDATA[Foobar]]></category>
		<category><![CDATA[php string functions]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[php upper cased strings]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[speaker]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[string functions]]></category>
		<category><![CDATA[United States]]></category>

		<guid isPermaLink="false">/?p=1984</guid>
		<description><![CDATA[The Task First of all what we&#8217;d like to achieve? The task is to convert a string, most of the cases single word, by capitalize the first letter. In my case I&#8217;ve the world countries names all lower cased, while I need them with first letter capitalized. In example &#8220;united states&#8221; must become &#8220;United States&#8221;, &#8230; <a href="/2010/09/17/5-php-string-functions-you-need-to-know/" class="more-link">Continue reading <span class="screen-reader-text">5 PHP String Functions You Need to Know</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2009/10/11/strict-types-in-zend-framework-and-in-php/" rel="bookmark" title="Strict types in Zend Framework and in PHP">Strict types in Zend Framework and in PHP </a></li>
<li><a href="/2011/08/17/php-fetch-get-as-string-with-http_build_query/" rel="bookmark" title="PHP: Fetch $_GET as String with http_build_query()">PHP: Fetch $_GET as String with http_build_query() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/09/string.jpg"><img class="aligncenter size-full wp-image-1991" title="strings in PHP" src="/wp-content/uploads/2010/09/string.jpg" alt="Strings in PHP" width="430" height="184" srcset="/wp-content/uploads/2010/09/string.jpg 430w, /wp-content/uploads/2010/09/string-300x128.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<h2>The Task</h2>
<p>First of all what we&#8217;d like to achieve? The task is to convert a string, most of the cases single word, by capitalize the first letter. In my case I&#8217;ve the world countries names all lower cased, while I need them with first letter capitalized. In example &#8220;united states&#8221; must become &#8220;United States&#8221;, but not &#8220;United states&#8221; or &#8220;UNITED STATES&#8221;. So here began the journey into PHP string functions, especially those for capitalization!</p>
<h2>1. ucwords</h2>
<p>The first thing you find in the <a title="PHP Manual" href="http://www.php.net/manual/en/index.php" target="_blank">PHP Manual</a> is the <a title="ucwords" href="http://php.net/manual/en/function.ucwords.php" target="_blank">ucwords</a> function. It changes the first letter to a capital letter, but it does not do the job. Why? Well let me show you an example.</p>
<pre lang="php">$str1 = 'foo bar';
$str2 = 'Foo bar';
$str3 = 'FOO BAR';
$str4 = 'фуу бар';

echo ucwords($str1); // Foo Bar
echo ucwords($str2); // Foo Bar
echo ucwords($str3); // FOO BAR
echo ucwords($str4); // фуу бар
</pre>
<p>Here we have four strings. A lower cased, an upper cased, a mixed cased and &#8230; a lower cased Cyrillic string. First of all the main reason why ucwords doesn&#8217;t fit here is because of the Cyrillic string. Whatever non-Latin string you have you can forget about capitalization. However the other strings conversions are also interesting. Take a look at the third string! Here the string remains &#8220;FOO BAR&#8221; instead of going &#8220;Foo Bar&#8221;, which simply means that this function only looks, and hopefully changes, the first letter.</p>
<p>So here we have two questions. How can we overcome the Cyrillic problem and how to &#8220;normalize&#8221; the UPPER CASE string?</p>
<h2>2. ucfirst</h2>
<p>This is another useful function in PHP. <a title="ucfirst" href="http://www.php.net/manual/en/function.ucfirst.php" target="_blank">ucfirst</a> as you may guess from its name converts a string by only changing its first letter. So &#8220;Foo bar&#8221; will remain &#8220;Foo bar&#8221;, while with ucwords it has become &#8220;Foo Bar&#8221;. Let&#8217;s see what this function does:</p>
<pre lang="php">$str1 = 'foo bar';
$str2 = 'Foo bar';
$str3 = 'FOO BAR';
$str4 = 'фуу бар';

echo ucfirst($str1); // Foo bar
echo ucfirst($str2); // Foo bar
echo ucfirst($str3); // FOO BAR
echo ucfirst($str4); // фуу бар
</pre>
<p>Here even the first string has only one capital letter &#8211; &#8220;foo bar&#8221; became &#8220;Foo bar&#8221;, and yet again we&#8217;ve the Cyrillic string unchanged. It simply doesn&#8217;t help us here!</p>
<h2>3. mb_convert_case</h2>
<p>As a PHP developer you know what the &#8220;mb_&#8221; prefix means &#8211; multibyte. This is quite useful. You can convert the string whatever the encoding is, so perhaps we can overcome the Cyrillic problem. But before proceeding to tests, let&#8217;s take a look at the parameters of this function.</p>
<p>The first thing to note here is that <a title="mb_convert_case" href="http://www.php.net/manual/en/function.mb-convert-case.php" target="_blank">mb_convert_case</a> doesn&#8217;t contain the case in its name &#8211; upper or lower. There&#8217;s a second parameter, after the first which is the string itself, who setups that. Note that here you don&#8217;t have the typical camel case or capitals parameter name, but MB_CASE_TITLE (as you know in English the title is always capitalized):</p>
<pre lang="php">echo mb_convert_case($str, MB_CASE_TITLE, ...
</pre>
<p>And a third one which specifies the encoding:</p>
<pre lang="php">echo mb_convert_case($str, MB_CASE_TITLE, 'utf-8')
</pre>
<p>Now let&#8217;s see what we can achieve with it:</p>
<pre lang="php">$str1 = 'foo bar';
$str2 = 'Foo bar';
$str3 = 'FOO BAR';
$str4 = 'фуу бар';

echo mb_convert_case($str1, MB_CASE_TITLE, 'utf-8'); // Foo Bar
echo mb_convert_case($str2, MB_CASE_TITLE, 'utf-8'); // Foo Bar
echo mb_convert_case($str3, MB_CASE_TITLE, 'utf-8'); // Foo Bar
echo mb_convert_case($str4, MB_CASE_TITLE, 'utf-8'); // Фуу Бар
</pre>
<p>As you can see now the Cyrillic problem doesn&#8217;t exists and mb_convert_case is intelligent enough to change &#8220;FOO BAR&#8221; into &#8220;Foo Bar&#8221; &#8211; as I said this is the English style titling. That is by no means the solution when you deal with capitalization with different encoding.</p>
<p>However there is another approach to overcome the all UPPER CASE conversion problem. A possible solution is to convert the string first to a lower case string.</p>
<h2>4. strtolower</h2>
<p><a title="strtolower" href="http://php.net/manual/en/function.strtolower.php" target="_blank">strtolower</a> is very useful PHP string function and perhaps any PHP developer has used it at least once. But yet again &#8211; it does not do the job. Again because of the encoding problem.</p>
<pre lang="php">echo strtolower('ФУУ БАР'); // #*&amp;$(#*%#
</pre>
<p>As you can see the Cyrillic string cannot be lower cased! Let&#8217;s search again into the &#8220;mb_&#8221; universe.</p>
<h2>5. mb_strtolower</h2>
<p>This is the function. Again you&#8217;ve to specify the encoding:</p>
<pre lang="php">
echo mb_strtolower('ФУУ БАР', 'utf-8')
</pre>
<h2>Conclusion</h2>
<p>It doesn&#8217;t matter whether you&#8217;re native English speaker or not. Most of the web sites are multilingual and you cannot be sure what happens when you convert strings in Alphabets different from the Latin. Thus be careful even when everything seems to be OK with Latin string tests.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2012/03/16/you-think-you-know-php-quiz-results/" rel="bookmark" title="You think you know PHP. Quiz Results!">You think you know PHP. Quiz Results! </a></li>
<li><a href="/2009/10/11/strict-types-in-zend-framework-and-in-php/" rel="bookmark" title="Strict types in Zend Framework and in PHP">Strict types in Zend Framework and in PHP </a></li>
<li><a href="/2011/08/17/php-fetch-get-as-string-with-http_build_query/" rel="bookmark" title="PHP: Fetch $_GET as String with http_build_query()">PHP: Fetch $_GET as String with http_build_query() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/17/5-php-string-functions-you-need-to-know/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Four Things to Know when Writing Comments</title>
		<link>/2010/06/27/four-things-to-know-when-writing-comments/</link>
		<comments>/2010/06/27/four-things-to-know-when-writing-comments/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 09:52:39 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Comment]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Source lines of code]]></category>
		<category><![CDATA[speaker]]></category>

		<guid isPermaLink="false">/?p=1668</guid>
		<description><![CDATA[Every developer has been learned from his teachers how important is to comment his source code. You should comment the classes, the methods, the logic, etc. However nobody explained how exactly to code with comments between the lines. Have you ever seen that i++; // we increment i with one That&#8217;s a shame! This doesn&#8217;t &#8230; <a href="/2010/06/27/four-things-to-know-when-writing-comments/" class="more-link">Continue reading <span class="screen-reader-text">Four Things to Know when Writing Comments</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/01/09/array-hint-in-javascript/" rel="bookmark" title="Array hint in JavaScript">Array hint in JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/06/how-to-write-comments.jpg"><img src="/wp-content/uploads/2010/06/how-to-write-comments.jpg" alt="How to write comments" title="how-to-write-comments" width="250" height="241" class="aligncenter size-full wp-image-1710" /></a><br />
Every developer has been learned from his teachers how important is to comment his source code. You should comment the classes, the methods, the logic, etc. However nobody explained how exactly to code with comments between the lines. Have you ever seen that</p>
<h2>i++; // we increment i with one</h2>
<p>That&#8217;s a shame! This doesn&#8217;t tell you anything, and what will happen if sometime later somebody sits in front of that code? He&#8217;d be amazed!</p>
<h2>How To</h2>
<p>In fact the task is not to determine what are the bad parts of not commenting the code, but how to overcome this. How to write comments. Here are some basic advices.</p>
<h3>1. Think about the other developers</h3>
<p>First of all the most important thing is to write comments with other programmers in mind. They should understand the logic of your code from your comments and not from the code itself.</p>
<h3>2. Write in English</h3>
<p>Even you&#8217;re not a native English speaker, and you English is not so good, it&#8217;s a must to write in English. This is really important when you work for a open source project when always there&#8217;s a big community around the project, you cannot be sure that everybody understands your language.</p>
<p>I know that this is difficult &#8211; also for me the English is not my native language, but that&#8217;s really a must. Once you start doing it, you&#8217;ll see that it&#8217;s not so difficult.</p>
<h3>3. Comment the methods as a black box</h3>
<p>Don&#8217;t write in a method comment what technique is used to achieve the result. It&#8217;s good to describe what the method does, not how it does it.</p>
<p>Bad example:</p>
<pre lang="php">
/**
 * returns $a + $b
 */
public function sum($a, $b) 
{
    return $a + $b;
}
</pre>
<p>Better:</p>
<pre lang="php">
/**
 * Returns the sum of two numbers
 * 
 * @param number $a
 * @param number $b
 * @returns number $a + $b
 */
public function sum($a, $b)
{
    return $a + $b;
}
</pre>
<h3>4. Comment the logic, not the technical solution</h3>
<p>When there&#8217;s a loop or a statement it&#8217;s useless to describe that this is a loop or statement, isn&#8217;t it? A better approach is to describe in a comment why you needed to implement this logic in such way.</p>
<p>Wrong:</p>
<pre lang="php">
...
$arr = array();
$i = 0;

// in a while loop we
// initialize every element with 0
while ($i < 100) {
    $arr[$i++] = 0; 
}
...
</pre>
<p>Better:</p>
<pre lang="php">
...
$arr = array();
$i = 0;

// we initialize an array of 0s
// to store the sorted elements
// on the first pass... etc, etc.
while ($i < 100) {
    $arr[$i++] = 0; 
}
...
</pre>
<h2>Conclusion</h2>
<p>In fact the most important thing is to write comments with other developers in mind. Tell the future developer of that code what's in the code, and pray for mercy <img src="https://s.w.org/images/core/emoji/11/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/05/24/javascript-objects-coding-style/" rel="bookmark" title="JavaScript Objects Coding Style">JavaScript Objects Coding Style </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/01/09/array-hint-in-javascript/" rel="bookmark" title="Array hint in JavaScript">Array hint in JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/27/four-things-to-know-when-writing-comments/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
