PHP curly braces in variable
Today I’ve got a discussion at stackoverflow about curly braces around PHP variable. The sample we’ve discussed about looked like this:
$query = "SELECT * FROM t WHERE smth LIKE '{$id}'"; |
In my opinion, it’s no sense for curly braces in this case. Imagine that you have an array or object and you need to access some array’s value or object’s variable. In this case, you will need curly brackets:
1 2 3 |
$query = "SELECT * FROM t WHERE smth LIKE '%{$array['key']}_jpg'"; // or $query = "SELECT * FROM t WHERE smth LIKE '%{$object->prop}_jpg'"; |
So, curly braces are needed to specify the begin and end of a variable.
Also, curly braces may be used for dynamic variables in PHP:
1 2 3 4 |
$a = 'smi'; $b = 'le'; $smile = ':-)'; echo ${$a.$b}; // will show :-) |
MySQL remote access
After MySQL’s been installed you can access it only from localhost (). It’s configured in my.cnf, in [mysqld] section. Here’s how this setting’s looked by default:
1 2 |
# only localhost
bind-address =
|
If bind-address is omitted or set to 0.0.0.0 all other clients will be able to access this MySQL server.
1 2 |
# all the clients
bind-address = 0.0.0.0
|
Specifying a certain IP address you say to MySQL server to listen connections only from this IP.
1 2 |
# for example, only IP
bind-address =
|
As for me, MySQL server isn’t started if I set a specific IP in bind-address
option. Maybe that’s because it bans connections from localhost. Okay, in this case, it’s possible to use a firewall to allow access for one or more IP addresses and set bind-address
to 0.0.0.0
.
So, to connect to your database remotely from any IP address you should allow your MySQL server to listen all the connections by commenting bind-address
or setting it to 0.0.0.0
. To enable a remote access only for a particular client you should set bind-address
to client’s IP or use a firewall. Using a firewall allows you to set access for one IP address or for multiple IP addresses.
Your changes to my.cnf
will be applied only after restarting MySQL server. To reboot MySQL server type this command:
1 2 |
# on Debian linux, in my case /etc/init.d/mysql restart |
Or:
1 2 |
# Ubuntu sudo service mysql restart |
Also, if you want to reboot the mysql server you may stop and then start the service:
1 2 3 4 5 |
# Stop sudo stop mysql # Start sudo start mysql |
Git core.editor for Windows
I’m using Windows 7. Here I’ll provide some steps of how to set up an editor to work with Git on Windows.
Step 1. Check what editor is set up at the moment.
git config --global --get core.editor |
Probably, you’ll get an empty value. Notice, that --global
parameter points to your current user .gitconfig
file.
Step 2. Edit your PATH
variable.
Now, it’s time to edit an environment variable PATH
. To do that open System Properties
dialog / Advanced
tab / Environment Variables...
/ System variables
/ PATH
. Edit PATH
variable and specify where to find an editor’s binary file. I use Notepad++ so in my case I specify a path to it.
%SystemRoot%\system32;%SystemRoot%;.....;C:\Program Files (x86)\Notepad++\ |
Step 3. Set up git core.editor.
To check that you can run your editor by specify only its name without specifying a path press Windows+R
and type notepad++
, or your editor’s name. An editor should be opened.
Let’s set up this editor to use it with git.
git config --global core.editor notepad++ |
Notepad++ factor
In my case, there was a problem with Notepad++. For example, you already have some opened tabs in Notepad++ and you’re working on some text files. When you’re trying to commit without a message, Git opens a text file in a new tab of Notepad++ and waits when you will close the text editor’s window. I don’t want to close the window with other tabs where I edit some other text files. To solve this problem set your text editor as follows:
git config --global core.editor "notepad++.exe -multiInst" |
By specifying -multiInst you tell Notepad++ to open new instance of editor. Before I set this options I got “Aborting commit due to empty comment message” when Notepad++ was opened and I was trying to make a commit.
Also, it’ll be more comfortable if you set these options:
git config --global core.editor "notepad++ -multiInst -nosession -noPlugin -notabbar" |
If you are interested in what these options do follow this .
How to test: if you are committing some changes without a comment git commit
, your text editor will be shown to specify a comment.
P.S. This approach works when you use cmd.exe
, Far
or something else that uses Windows environment. This won’t work if you use Git Bash.
Script that positions html element over YouTube video
In the previous article we discussed how to position HTML elements over YouTube video. The problem is that YouTube video overlaps any html element, by default. How to force YouTube video showing behind HTML elements with help of HTML approach read in following article How to position html elements over YouTube video.
Here you’ll see how to perform the same but with JavaScript approach. First, script finds all the YouTube iframes and then adds wmode parameter to them. This wmode parameter should have transparent or opaque values.
1 2 3 4 5 |
jQuery(function ($) { $("iframe[src]").each(function () { $(this).prop("src", $(this).prop("src") + "?wmode=transparent"); }); }); |
In this script jQuery of 1.8.2 is used. Note that this fix is needed for old browsers, for example, IE9 and older, some verstions of FireFox.
How to position html elements over YouTube video
In this article you’ll find how to force YouTube video showing under your html elements.
By default, YouTube video is showed over any html element. In my case, the problem refers to Dropdown Menu and Lightbox. For example, when Lightbox is appearing YouTube video overlaps it ignoring z-index. In general, you may run into this problem with any html element you want to position over YouTube video.
At the moment, YouTube video is added to html page via iframe. Inside the iframe YouTube creates all the stuff it needs to show the video. In the depth of iframe there’s Flash Player, by default. If HTML 5 is used to show the video there isn’t any Flash in iframe and there isn’t any problem with overlay. Flash is inserted to the browser using
helpx.adobe.com/flash/kb/flash-object-embed-tag-attributes.html
After reading the docs you can find an interesting attribute wmode and an interesting possible value of this attribute – transparent:
transparent – The SWF content is layered together with other HTML elements on the page.
That’s what we need! So, SWF content is Flash that we want to behave like other html elements.
Here is a schematic html code YouTube uses to show the video deep inside iframe:
1 |
="application/x-shockwave-flash" src="..."> |
Html should be like following to fix the problem with overlay:
1 |
"transparent" type="application/x-shockwave-flash" src="..."> |
Fortunately, it’s possible to toggle wmode attribute via iframe source parameters when inserting YouTube video to html:
1 2 3 |
<iframe width="560" height="315" src="http://www.youtube.com/embed/NeXMxuNNlE8?wmode=transparent" frameborder="0" allowfullscreen></iframe>? |
Take notice at src attribute and wmode parameter:
src=”http://www.youtube.com/embed/NeXMxuNNlE8?wmode=transparent“
Now, using this approach it’s possible to position Menu, Lightbox or any other html element over YouTube video or other flash/swf movie.
This problem appears not in all browsers, mostly in old browsers like IE, old versions of FireFox or WebKit. The test example is here:
Get image resolution in PHP
Here’s a code snippet in PHP of how to get image resolution. I mean image width and height.
There’re lots of approaches of doing that and I’ve tryed to find the short one.
1 2 |
list($width, $height, $type, $attr) = getimagesize($path_to_image); // then you may use $width and $height variables |
So, to get the image resolution you should write only one line of PHP code. Also, you can get other info about the image using the getimagesize function. $path_to_image
is a path to the image, for example, “/tmp/sunset.jpg” or “C:\Temp\image.jpg”, and in your case, it’s a path to the image the resolution of which you want to get. You can find more help about this function in PHP documentation.
To use this function the GD library is required. You don’t need any other frameworks or third party libraries.
Apache could not be started because of mod_ssl
Everything was working well. But maybe a week ago, our system administrator was installing some stuff on all the machines for internal needs. After that my Apache web server hasn’t been started. Today, I needed to test something with PHP and I needed Apache for that. So, I started to investigate.
Here’s what Apache said:
Apache 2 is starting …
(OS 10048) Only one usage of each socket address (protocol/network address/port) is normally permitted. : AH00072: make_sock: could not bind to address [::]:4433.
(OS 10048) Only one usage of each socket address (protocol/network address/port) is normally permitted. : AH00072: make_sock: could not bind to address 0.0.0.0:4433.
AH00451: no listening sockets available, shutting down
AH00015: Unable to open logs
Apache could not be started
I’ve tryed to change a port number, but nothing changed. BTW, I don’t need SSL for my work, so I just tryed to comment this module in httpd.conf:
#LoadModule ssl_module modules/mod_ssl.so
After commenting mod_ssl.so Apache starts without any errors. So, it’s a workaround. But I still don’t know what’s the true problem.
Problem with HTML autocomplete on Android browser
I’ve just fixed very evil bug.
My web site works well on all the major browsers. But when I tested it on Android phone, using standard Android browser, I was surprised.. In some text fields or inputs, that browser sets my email. By the way, there’s nothing about email in the input name or id. Again, all the major desktop browsers don’t have this bug.
Despite on the setted value attribute for the input, Android browser’s autocomplete still sets its value. So, it even ignores the value
attribute prefering the autocomplete’s value.
PHP:
echo $n ?>"> |
Then, in HTML:
<input type="text" name="n" value="qwerty">
|
But Android browser sets my email to this field.
The fix of this problem is simple. I’ve just set autocomplete=”off” attribute for the inputs!
<input type="text" name="n" value="" autocomplete="off">
|
WideImage performance and memory issues
Hi! in the previous post Trying to resize a set of large images in PHP I wrote that WideImage takes a lot of memory to resize big images, it was around 140MB and WideImage was resizing my images during ~30 seconds.
I’ve been thinking about this performance and memory issues and decided to write some PHP code that do the same but without WideImage library. I’ve used these PHP functions getimagesize, imagecreatefromjpeg, imagecreatetruecolor, imagecopyresized, imagejpeg and imagedestroy.
To my surprise, my custom PHP code do the same during ~15 seconds and takes only 250KB of memory!!! So, I think there are some problems with performance and memory in WideImage library. For me, the custom code that works faster and takes very less memory is a great solution.
Trying to resize a set of large images in PHP
Well, my PHP script terminates if I try to resize a set of big images. I use WideImage to resize images. Each image in a set is weighted around 10M. When I start my PHP script it terminates halfway and only several images from a collection have been resized. So, maybe it’s some limitation.. I am stating to find something in php.ini that starts with words “max..”. Wow! I’ve found memory_limit it’s a maximum amount of memory a script may consume. The default value of this setting is 128M. I set the new value of 500M for test. OK! My script has resized all the images! The amout of memory that is allocated is 140M, so it’s greater then a default value of 128M.
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit ; memory_limit = 128M memory_limit = 256M |
I also log a running time of my script and it’s 28 seconds. In the php.ini the default value of maximum execution time is 30 seconds. To be on the safe side, I increased it to 60 seconds.
; Maximum execution time of each script, in seconds ; http://php.net/max-execution-time ; Note: This directive is hardcoded to 0 for the CLI SAPI ; max_execution_time = 30 max_execution_time = 60 |